phingoc
phingoc

Reputation: 101

Unable to save data to db

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;    
    }

Upvotes: 0

Views: 34

Answers (2)

phingoc
phingoc

Reputation: 101

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.

Upvotes: 0

wooknight
wooknight

Reputation: 124

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.
}

Upvotes: 3

Related Questions