else语句不返回

xmjla07d  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(386)

我正在编写一个脚本,检查数据库中是否存在电子邮件 barn_users ,以重置密码。如果电子邮件不存在,则不会生成令牌/代码并将其存储在数据库中。一切都很正常,直到我输入了一封不在系统中的电子邮件。错误为“在系统中找不到电子邮件”的else语句不会触发。我为什么不明白?

$sql_1 = "SELECT * FROM password_reset WHERE email='$email'";
    $sql_2 = "SELECT * FROM barn_users WHERE email='$email'";

    $generate = mysqli_query($conn, $sql_1);
    $searchEmail = mysqli_query($conn, $sql_2);
    while($row = mysqli_fetch_array($searchEmail)) {
            if(mysqli_num_rows($searchEmail) > 0) {
                if (mysqli_num_rows($generate) > 1) {
            } else if (mysqli_num_rows($generate) < 1) {
                $sql = "INSERT INTO password_reset (code,email) VALUES ('$code','$email')";
            } else {
                $sql = "UPDATE password_reset SET code='$code' WHERE email='$email'";
            }   
            if ($conn->query($sql) == TRUE) {
                echo "Reset password has been emailed to you"; 
            } else {

            }
        } else {
            echo "Could not find email in the system.";
        } 
    }
j1dl9f46

j1dl9f461#

感谢@bleat和@ivan的评论。你的回答都有帮助。这是工作结果。在循环外使一个变量为假,给了我想要的答案。

$sql_1 = "SELECT * FROM password_reset WHERE email='$email'";
    $sql_2 = "SELECT * FROM barn_users WHERE email='$email'";

    $generate = mysqli_query($conn, $sql_1);
    $searchEmail = mysqli_query($conn, $sql_2);

    $found = false;

    while($row = mysqli_fetch_array($searchEmail)) {
        $found = true;
        if(mysqli_num_rows($searchEmail) > 0) {
            if (mysqli_num_rows($generate) > 1) {

            } else if (mysqli_num_rows($generate) < 1) {
                $sql = "INSERT INTO password_reset (code,email) VALUES ('$code','$email')";
            } else {
                $sql = "UPDATE password_reset SET code='$code' WHERE email='$email'";
            }   
            if ($conn->query($sql) == TRUE) {
                echo "Reset password has been emailed to you"; 
            } else {

            }
        } else {

        }
    }

    if ($found == false) {
        echo "Sorry, that email was not found in the system. Please try again.";
    }

相关问题