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

时间:2011-12-15 02:39:55

标签: php system registration

我在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 个答案:

答案 0 :(得分:3)

您忘记包装嵌套的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;
}

你忘记了大括号