検証と成功時に特定の 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>";