没有PHP错误,mysqli_query无法正确插入

时间:2018-08-29 16:55:45

标签: php mysqli

我已经盯着代码一个多小时了,冒着白痴的危险,如果有人能在这里指出我的错误,我将不胜感激。

<?php

#define variables and set to empty values
$first = $last = $username = $email = $password = $password_check = "";


#check if the user has clicked the button
if (isset($_POST["submit-register"])){

    #get the connection to database
    require "dbh.inc.php";

    #retrieve information
    $first = mysqli_real_escape_string($conn, $_POST["first"]);
    $last = mysqli_real_escape_string($conn, $_POST["last"]);
    $username = mysqli_real_escape_string($conn, $_POST["username"]);
    $email = mysqli_real_escape_string($conn, $_POST["email"]);
    $password = mysqli_real_escape_string($conn, $_POST["password"]); 
    $password_check = mysqli_real_escape_string($conn, $_POST["password_check"]);

#Error handlers

#Check for empty fields, not sure if necessary due to JavaScript
    if (empty($first) || empty($last) || empty($username) || 
        empty($email) || empty($password) || empty($password_check)){
        header("Location: ../register.php?not_all_fields_were_filled_in");
        exit();
    } else { #check for valid username
        if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)){
        header("Location: ../register.php?signup=invalid");
        exit();
        } else { #check for matching passwords
            if ($password != $password_check){
            header("Location: ../register.php?passwords_do_not_match");
            exit();
            } else { #check for correct email address
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                header("Location: ../register.php?signup=invalidemail");
                exit();
                } else { #check if the user already exists
                    $sql = "SELECT * FROM users WHERE username='$username' OR email='$email'";
                    $result = mysqli_query($conn, $sql);
                    $resultCheck = mysqli_num_rows($result);
                    if ($resultCheck > 0) {
                    header("Location: ../register.php?signup=username_or_email_taken");
                    exit();
                    } else { #hash password and insert user
                        $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                        $sql = "INSERT INTO users (Name_First, Name_Last, Username, Email, Password) 
                        VALUES ('$first', '$last', '$username', '$email', '$hashedPwd');";
                        mysqli_query($conn, $sql);
                        header("Location: ../register.php?signup=success");
                        exit();
                        }
                    }
                }
            }
        } 
    } else {
        header("Location: ../register.php");
        exit();
        }

如果我填写所有字段并提交,则URL会显示“ localhost /...../ signup = succes,这意味着所有if语句均正常运行。我通过将字段保留为空白,输入不同的密码来再次检查此内容,无效的电子邮件,一切。

当我在phpmyadmin中检查数据库时,它没有更新。我真的不明白为什么会这样。我也找不到任何错别字。

0 个答案:

没有答案