Search code examples
phpmysqldatabaseregistration

PHP registration not inserting new user into table


I'm following a tutorial and I've run into an issue where I can complete my registration form but my info isn't saved into the database table. All my code is the same as the tutorial. Am I missing something?

Obviously it has to do with my $insert variable, but I can't figure out what it is.

if(isset($_POST['register'])) {
        $user_name = mysqli_real_escape_string($con, $_POST['user_name']);      
        $user_pass = mysqli_real_escape_string($con, $_POST['user_pass']);
        $user_email = mysqli_real_escape_string($con, $_POST['user_email']);
        $user_country = mysqli_real_escape_string($con, $_POST['user_country']);
        $user_number = mysqli_real_escape_string($con, $_POST['user_number']);
        $user_address = mysqli_real_escape_string($con, $_POST['user_address']);
        $user_gender = mysqli_real_escape_string($con, $_POST['user_gender']);
        $user_b_day = mysqli_real_escape_string($con, $_POST['b_day']);

        $user_image = $_FILES['user_image']['name'];
        $user_tmp = $_FILES['user_image']['tmp_name'];

        if($user_address=='' OR $user_country=="" OR $user_image=="" OR $user_gender=='') {
            echo "<script>alert('Please fill all the fields.')</script>";
            exit();
        }

        if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)) {
            echo "<script>alert('Your email is not valid.')</script>";
            exit();
        }
        $sel_email = "SELECT * FROM register_user WHERE user_email='" . $user_email . "';";
        $run_email = mysqli_query($con, $sel_email);        

        $check_email = mysqli_num_rows($run_email);

        if($check_email==1) {
            echo "<script>alert('This email is already registered. Please choose another.')</script>";
            exit();
        }

        else {
            $_SESSION['user_email'] = $user_email;

            move_uploaded_file($user_tmp, "images/$user_image");

            $insert = "INSERT INTO register_user (user_name, 
                                                user_pass, 
                                                user_email, 
                                                user_country, 
                                                user_number, 
                                                user_address, 
                                                user_gender, 
                                                user_b_day, 
                                                user_image, 
                                                register_date) 
                        VALUES ('$user_name', 
                                '$user_pass', 
                                '$user_email', 
                                '$user_country', 
                                '$user_number', 
                                '$user_address', 
                                '$user_gender', 
                                '$user_b_day', 
                                '$user_image', 
                                 NOW())";

            mysqli_query($con, $insert);

            echo "<script>alert('Registration successful.')</script>";
            echo "<script>window.open('home.php', '_self' )</script>";
        }
    }

Solution

  • If you have no error message, try this:

       $insert = "INSERT INTO register_user (user_name, 
                                                    user_pass, 
                                                    user_email, 
                                                    user_country, 
                                                    user_number, 
                                                    user_address, 
                                                    user_gender, 
                                                    user_b_day, 
                                                    user_image, 
                                                    register_date) 
                            VALUES ('".$user_name."', 
                                    '".$user_pass."', 
                                    '".$user_email."', 
                                    '".$user_country."', 
                                    '".$user_number."', 
                                    '".$user_address."', 
                                    '".$user_gender."', 
                                    '".$user_b_day."', 
                                    '".$user_image."', 
                                     NOW())";