1

For some reason my PHP login script keeps returning "invalid email/password combination", yet i know I am entering the correct email and password. Does anyone see what I might be doing wrong?

<?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";
}

?>
4

4 回答 4

2

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

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

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

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

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

于 2012-04-10T23:33:30.583 回答
2

我会转而说,

if (mysqli_num_rows($result) > 0)

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

于 2012-04-10T23:30:24.143 回答
1

you care getting that when mysqli_num_rows ( $result ) is not equal to 1 ... What i think you should do is verify if the username and password exist directly in the database ..... that is the only logical reason in your script why you should be getting invalid username/password combination

If you still have additional issue let me know

于 2012-04-10T23:25:56.753 回答
0

出现错误是因为数据库中有重复的数据。

于 2012-04-10T23:53:48.377 回答