【问题标题】:PHP code show still with error even if all input are correct. What's wrong with my code?即使所有输入都正确,PHP 代码仍然显示错误。我的代码有什么问题?
【发布时间】:2011-12-15 02:39:54
【问题描述】:

我在 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;
}
?>

【问题讨论】:

  • 当我看到 PHP 代码没有使用 PHP Prepared Statements 来防止 SQL Injection 漏洞时,我很担心。我希望您正在清理尚未粘贴到此处的代码中的变量。如果没有,请考虑重写代码以使用 PDO 准备语句,而不是尝试清理您的变量。

标签: php system registration


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    if (mysqli_num_rows($result1) < 0)
    {
         echo "email is already used. ";
         $withError = true;
    }
    

    你忘了大括号

    【讨论】:

      猜你喜欢
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      相关资源
      最近更新 更多