0

可能重复:
在数据库中找不到正确的电子邮件地址

我的 if else 语句有问题。当我在文本框中输入正确的用户名和电子邮件时,它会继续执行 else 语句并回显“您输入了错误的电子邮件地址”,而不是执行 if 语句并回显“您的详细信息正确”。即使我从数据库中输入了正确的用户名和与该用户相关联的正确电子邮件,有谁知道为什么它会回显错误的语句?

if (isset($_POST['resetbtn'])) {
    //get form data
    $user  = $_POST['user'];
    $email = $_POST['email'];

    //make sure info is provided
    if ($user) {
        if ($email) {
            if ((strlen($email) >= 7) && (strstr($email, "@")) && (strstr($email, "."))) {

                $query   = "SELECT TeacherUsername, TeacherEmail FROM Teacher WHERE TeacherUsername = ?";
                // prepare query
                $stmt    = $mysqli->prepare($query);
                // You only need to call bind_param once
                $stmt->bind_param("s", $user);
                // execute query
                $stmt->execute();
                // get result and assign variables (prefix with db)
                $stmt->bind_result($dbTeacherUsername, $dbTeacherEmail);
                //get number of rows
                $stmt->store_result();
                $numrows = $stmt->num_rows();

                if ($numrows == 1) {

                    $row            = $stmt->fetch();
                    $dbTeacherEmail = $row['TeacherEmail'];

                    //make sure email is correct

                    if ($email == $dbTeacherEmail) {

                        echo "Your details are correct";
                    } else {
                        echo "You entered in the Wrong Email Address";
                    }
                } else {
                    echo "Please Enter in your Email";
                }
            } else {
                echo "Please Enter in your Username";
            }
        }
    }
}
4

2 回答 2

1

在 php 中比较字符串时不要使用 ==。

利用

strcmp($string1,$string2)==0

来测试平等。

于 2012-09-04T13:57:56.013 回答
0

我认为你使用$stmt->fetch()不正确。您似乎将返回值视为一个数组,但返回是一个布尔值,并且 的值$dbTeacherEmail已经绑定到结果 by $stmt->bind_result($dbTeacherUsername, $dbTeacherEmail)

尝试删除读取的行

$dbTeacherEmail = $row['TeacherEmail'];

看看你是否得到你想要的。另请参阅mysqli-stmt::fetch()上的文档

于 2012-09-04T20:50:47.697 回答