了解嵌套如果.其他语句


Understanding Nested If.. Else statements

由于某种原因,我的PHP登录脚本不断返回"无效的电子邮件/密码组合",但我知道我输入了正确的电子邮件和密码。有人看到我可能做错了什么吗?

<?php
$email= $_POST['email'];
$password= $_POST['password'];
if (!empty($email) && !empty($password)) {
    $connect= mysqli_connect("localhost", "root", "", "si") or die('error connecting with the database');
    $query= "SELECT user_id, email, password FROM users WHERE email='$email' AND            
    password='$password'";
    $result= mysqli_query($connect, $query) or die('error with query');
    if (mysqli_num_rows($result) == 1) {
        $row= mysqli_fetch_array($result);
        setcookie('user_id', $row['user_id']);
        echo "you are now logged in";
    } else {
        echo "invalid username/password combination";
    }
} else {
    echo" you must fill out both username and password";
}
?>

我会把它换成:

if (mysqli_num_rows($result) > 0)

但除此之外,代码看起来应该可以工作。您确定 $_POST 变量包含您认为它们应该包含的内容吗?您确定数据库中存在此用户/通行证组合吗?

你的代码看起来不错。我会检查以下内容:

确保查询正常工作。我会回显查询并在数据库程序中的数据库上运行它,看看结果是什么。

确保您没有超过 1 个用户使用该电子邮件/密码组合 - 它会干扰您的计数检查。

检查您发布的详细信息,并确保它们与您在数据库中的内容正确(没有杂散的空格等)。

最后,你最好正确地缩进和布局你的代码,看看在这里阅读起来有多容易:http://codepad.org/DmtMuTpC

mysqli_num_rows ( $result )不等于1时,你关心得到它...... 我认为您应该做的是验证usernamepassword是否直接存在于数据库中.....这是脚本中唯一合乎逻辑的原因,为什么你应该得到invalid username/password combination

如果您还有其他问题,请告诉我

出现错误是因为数据库中存在重复数据。