php - MySQL语句好像有错误。我收到 mysqli_num_rows() 和 mysqli_fetch_array() 错误

标签 php mysql

我一直在制作密码重置表单。我的 SQL 语句似乎没有返回任何内容。我删除了部分代码以简化它,但我似乎无法粘贴 mysqli_num_rows()。我认为我的 mysqli_query() 语句有问题。我尝试使用 mysql_num_rows 和 mysql_query 替代方案。

<?php
if(isset($_POST["u"]))
{
// CONNECT TO THE DATABASE
    include_once("includes/connect.php");

// GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE 
    $u = preg_replace('#[^a-z0-9_]#i', '', $_POST['u']);         
    $e = mysql_real_escape_string($connect, $_POST['e']);

$username = $_POST['username'];
$email = $_POST['email'];

$query = mysqli_query($connect, "SELECT username, email FROM users WHERE username='$username'");
$numrows = mysqli_num_rows($query);
if($numrows > 0)
        {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){

        $db_u = $row["username"];
                    $db_e = $row["email"];
    }    
            if($e == $db_e && $u == $db_u)
                {
        $code = rand(10000,9999999);

        $sql = "UPDATE users SET passreset='$code' WHERE username='$username'";
        $query = mysqli_query($connect, $sql);            

        $to = $db_email;
        $subject = "Password Reset";
        $body = "This is an automated email 

        ";


        mail($to,$subject,$body);

        echo "success";

            }
 else {
 echo"no_exist";
 exit();

 }
}
}
?>



<script>
function forgotpass(){
    var u = _("username").value;
var e = _("email").value;
if(e == "" || u == ""){
    _("status").innerHTML = "This form requires you enter both an username and email.";
} else {
    _("forgotpassbtn").style.display = "none";
    _("status").innerHTML = 'please wait ...';
    var ajax = ajaxObj("POST", "forgot.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            var response = ajax.responseText;
            if(response == "success"){
                _("forgotpassform").innerHTML = '<h3>Check your email inbox </h3>';
            } else if (response == "no_exist"){
                _("status").innerHTML = "Email address not found";
            } else if(response == "email_send_failed"){
                _("status").innerHTML = "Mail function failed to execute";
            } else {
                _("status").innerHTML = "An unknown error occurred";
            }
        }
    }
    ajax.send("e="+e+"&u="+u);
}
}
</script>
</head>
<body>


  <h3>Reset password</h3>
 <form id="forgotpassform" onsubmit="return false;">
 <div>Enter Username</div>
<input id="username" type="text" onfocus="_('status').innerHTML='';" maxlength="88">  
<br /><br />

<div>Enter Your Email Address</div>
<input id="email" type="text" onfocus="_('status').innerHTML='';" maxlength="88">
<br /><br />

<button id="forgotpassbtn" onclick="forgotpass()">Submit</button> 
<p id="status"></p>
 </form>
  </div>

    </body>
    </html>

最佳答案

变量 $e 的值可能是一个问题,因为您正在使用带有两个变量的 mysql 转义,请将其更改为 mysqli 转义

关于php - MySQL语句好像有错误。我收到 mysqli_num_rows() 和 mysqli_fetch_array() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36712108/

相关文章:

php - 我的 CI 无法正确运行查询

php - 如何使用 php 分别插入照片和文本?

php - 如何制作 Firefox,不缓存我的网站

php - 使用 MYSQL 选择日期之间的行?

php - 将 MySQL 数据标记为 'read'

php - 从 PHP 运行 at 没有输出

php - 向电子邮件地址来自 Mysql 的不同用户发送邮件

mysql - mysql从数据库中获取结果id?

mysql - 如何从 2x LEFT JOIN 计算字段数据

c++ - 在 C++ 中序列化对象并在 mysql 中存储为 blob 类型