PHP mysql bug查询

时间:2013-08-24 03:04:46

标签: php mysql

嗨,大家好我已经工作了12个小时,我似乎找不到修复。 我正在尝试将用户输入与数据库结果进行比较,例如$username == $result echo“已经采用了用户名,但问题是它没有中断地传递2个语句,如果我暂停退出循环,尽管没有在现场输入任何电子邮件,它仍会检查$email == $result2

if (isset($_POST['username']) or isset($_POST['email'])) {
    $extract = mysql_query("
        SELECT
          `username`, `email`
        FROM `users`
        WHERE `username`='$username' OR `email`='$email'
    ");

    $resultq = mysql_num_rows($extract);
    while ($row = mysql_fetch_array($extract)) {

        $result = $row['username'];
        $result2 = $row['email'];

        echo " " . $result;
        echo " " . $result2;

        if ($username == $result) {
            echo " Username is already Taken!";
            //  break; //whenever i put break, it always gives me the else if statement echo, despite not entering any email in the field
        } //$pass = $_POST['pass'];
        else if ($email == $result2) {
            echo "Email Address is already used!";
            //  break;
        } else {
        }
    }
}

2 个答案:

答案 0 :(得分:2)

从mysql升级到MySQLi或PDO。

然而;

$extract= mysql_query("SELECT username, email FROM users where username='$username' or email='$email'");

$resultq = mysql_num_rows($extract);
    if($resultq > 0) {
       echo 'Either your username, or email is already taken!';
       return;
    }

答案 1 :(得分:0)

请参阅sniko了解您最初问题的答案。但是,请参阅以下内容......

您没有定义$ username或$ email

if ($username == $result )应该是

if ($_POST['username'] == $result ) 

或者您可以在条件开头定义$ username和$ email。

你也应该改变这个:

isset($_POST['username']) or isset($_POST['email'])

到此:

isset($_POST['username']) && isset($_POST['email'])

因为您在查询中依赖于这两者。

正如sniko所说,切换到mysqli或PDO。不推荐使用mysql_。

完整样本:

if(isset($_POST['username']) && isset($_POST['email']))
{
    $username = $_POST['username'];
    $email = $_POST['email'];

    $extract= mysql_query("SELECT username, email FROM users where username='$username' or email='$email'");

    $resultq = mysql_num_rows($extract);
    if($resultq > 0) {
       echo 'Either your username, or email is already taken!';
       return;
    }

}

另请注意,您没有清理输入。您可能会被此输入攻击。切换到mysqli或pdo并使用预处理语句会有所帮助。