即使所有输入都正确,PHP代码仍会显示错误。我的代码出了什么问题?

时间:2021-11-18 08:09:01

I'm making a simple registration system in php however, even if i enter all correct details, it doesn't go to the database, it still echo that withError = 1 even if all entries are correct. Here is my php code.

我在php中创建了一个简单的注册系统,但即使我输入了所有正确的细节,它也没有进入数据库,即使所有条目都正确,它仍会回显withError = 1。这是我的PHP代码。

<?php
if($submit)
{
    if ($fullname && $email && $username && $password && $conPassword)
    {
        if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
        {
            $query1 = "SELECT Email FROM tbl_userAccounts WHERE Email='$email'";
            $result1 = mysqli_query($mysqli,$query1) or die(mysqli_error());
            if (mysqli_num_rows($result1) < 0)
            echo "email is already used. ";
            $withError = true;
        }
        else
        {
            echo "invalid email address. ";
            $withError = true;
        }
        if (strlen($username) < $charMinimum)
        {
            echo "minimum of 6 characters for username. ";
            $withError = true;
        }
        else
        {
            $query2 = "SELECT Username FROM tbl_userAccounts WHERE Username='$username'";
            $result2 = mysqli_query($mysqli,$query2) or die(mysqli_error());
            if (mysqli_num_rows($result2) < 0)
            {
                echo "username is already used. ";
                $withError = true;
            }
        }
        if($password == $conPassword)
        {
            echo $withError;
            if($withError == false)
            {
                if (strlen($password) < $charMinimum)
                {
                    echo "minimum of 6 characters for password. ";
                    $withError = true;
                }
                else
                {
                    $query3 = "INSERT INTO tbl_userAccounts VALUES ('', '$fullname', '$username', '$password','$date', '$email')";
                    $result3 = mysqli_query($mysqli,$query3) or die(mysqli_error());
                    if($result3 && $withError == false)
                    echo "account has been successfully registered!";
                    else
                    echo "failed registration. ";
                }
            }
        }
        else
        {
            echo "passwords do not match. ";
            $withError = true;
        }
    }
}
else
{
    echo "fill out all fields. ";
    $withError = true;
}
?>

2 个解决方案

#1


0  

if (mysqli_num_rows($result1) < 0)
{
     echo "email is already used. ";
     $withError = true;
}

You forgot the curly bracket

你忘记了花括号

#2


3  

You forgot to wrap the nested IF statement.

你忘了包装嵌套的IF语句。

    if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
    {
        $query1 = "SELECT Email FROM tbl_userAccounts WHERE Email='$email'";
        $result1 = mysqli_query($mysqli,$query1) or die(mysqli_error());
        if (mysqli_num_rows($result1) < 0) {
            echo "email is already used. ";
            $withError = true;
        }
    }

#1


0  

if (mysqli_num_rows($result1) < 0)
{
     echo "email is already used. ";
     $withError = true;
}

You forgot the curly bracket

你忘记了花括号

#2


3  

You forgot to wrap the nested IF statement.

你忘了包装嵌套的IF语句。

    if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
    {
        $query1 = "SELECT Email FROM tbl_userAccounts WHERE Email='$email'";
        $result1 = mysqli_query($mysqli,$query1) or die(mysqli_error());
        if (mysqli_num_rows($result1) < 0) {
            echo "email is already used. ";
            $withError = true;
        }
    }