我在下面有一段代码,它在验证和成功时执行某些 php/mysqli 任务。我在下面的代码中遇到的问题是,如果用户输入了由 SELECT 查询确定的不正确的用户名或电子邮件,如果没有匹配的用户名和电子邮件,那么它应该显示错误消息Your Username or Email was not Correct
。但相反,它显示此错误消息An error has occured, your Email was not sent containing your new Password
。
我的问题是为什么它显示不正确的错误消息,我怎样才能让它显示正确的错误消息?
if(isset($_POST['resetbtn'])){
//get form data
$user = $_POST['user'];
$email = $_POST['email'];
$errors = array();
if(!$errors) {
$query = "SELECT TeacherUsername, TeacherEmail FROM Teacher WHERE TeacherUsername = ? AND TeacherEmail = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("ss",$user, $email);
// 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){
$pass = rand();
$teacherpassword = md5($pass);
$teacherpassword = substr($pass, 0, 15);
$teacherpassword = md5(md5("g3f".$pass."rt4"));
//update password in db
$updatesql = "UPDATE Teacher SET TeacherPassword = ? WHERE TeacherUsername = ?";
$update = $mysqli->prepare($updatesql);
$update->bind_param("ss", $teacherpassword, $user);
$update->execute();
$query = "SELECT TeacherUsername, TeacherPassword FROM Teacher WHERE TeacherUsername = ? AND TeacherPassword = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("ss",$user,$teacherpassword);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbTeacherUsername, $dbTeacherPassword);
//get number of rows
$stmt->store_result();
$selectnumrows = $stmt->num_rows();
}else{
if(!$numrows){
$errormsg = "Your Username or Email was not Correct";
$user = "";
$email = "";
}
}
}
if(empty($errors)) {
if ($selectnumrows == 1){
$errormsg = "<span style='color: green'>Your Password has been Reset. An Email has been sent with your New Password</span>";
else{
$errormsg = "An error has occured, your Email was not sent containing your new Password";
}
}
下面是表格:
echo "<form action='./forgotpass.php' method='post'>
<table>
<tr>
<td></td>
<td id='errormsg'>$errormsg</td>
</tr>
<tr>
<td>Username</td>
<td><input type='text' name='user' value='$user'/><br/>".$error_user."</td>
</tr>
<tr>
<td>Email</td>
<td><input type='text' name='email' value='$email'/><br/>".$error_email."</td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='resetbtn' value='Reset Password' /></td>
</tr>
</table>
</form>";