0

Ok, so I shouldn't have this problem, but still, I'm struggling to find out where I go wrong. :(

Trying to save some data to db after checking if email already exists and password matches.

Can someone see the fault somewhere, because I have tried for several hours now. When running this message it returns "something went wrong".

    function register_new_user($name, $company, $email, $password, $cPassword) {
        global $connect;

        //CHECK IF EMAIL EXISTS
        $query = "SELECT * FROM users WHERE email='$email'";
        $result = mysqli_query($connect, $query);
        if(mysqli_num_rows($result) >= 1) {
            $message = "Epost adresse er allerede registerert. Har du glemt passordet ditt? Trykk her for å gjenoprette.";
        } else {
            //CHECK IF PASSWORD MATCH
            if($password != $cPassword) {
                $message = "Passordene er ikke like. Prøv igjen";
            } else {
                //HASH PASSWORD
                $hashPassword = password_hash($password, PASSWORD_BCRYPT);
                //GENERATE DATE AND CODE
                $date = date('d.m.Y');
                $code = rand(1000000000, 9999999999);
                //ESCAPE THE INPUTS
                $name_esc = mysqli_real_escape_string($connect, $name);
                $company_esc = mysqli_real_escape_string($connect, $company);
                $email_esc = mysqli_real_escape_string($connect, $email);
                //SAVE TO DB
                $query = "INSERT INTO users (name, company, email, password, code, date) VALUES ('$name_esc', '$company_esc', '$email_esc', '$hashPassword', '$code', '$date') ";
                $result = mysqli_query($connect, $query);
                    if(!$result) {
                        $message = "Something wrong"; //this get returned from function.
                    } else {
                        $message = "OK";
                    }
                mysqli_close($connect);
            } 
        }
    return $message;    
    }
2
  • 1
    can you post the error you are having? Commented Feb 26, 2019 at 20:46
  • 1
    date is a mysql keyword you need to use backticks Commented Feb 26, 2019 at 20:51

2 Answers 2

3

Can you try this and see what mysql is complaining about ??

if(!$result)  {
    printf("Error: %s\n", mysqli_error($connect));
    $message = "Something wrong"; //this get returned from function.
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Added the code and found out that i tried to save a string to int column. :/ Changed the column to string, and it works.
0

Stupid as I am, I went a bit fast when creating the table in DB. The fault was that i tried to save a string into a Int column in the database. And ofcourse didnt bother to check the table structure when debugging.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.