我正在按照nickfrosty制作的关于如何设置忘记密码功能的精彩教程,如果你想看看这个视频,请转到youtube并输入“Nickfrosty php忘记密码”,你会发现它。
他与我的忘记密码功能之间的唯一区别是他在mysql上做了他的,而我正在使用mysqli。
现在我收到的问题是,在输入正确的用户名和电子邮箱中的文本框后,它会一直显示回显“发生错误,密码未重置。现在服务器运行正常,因为我创建了注册功能,用户在其注册详细信息中输入,并在提交后通过服务器发送电子邮件。
那么我的问题是,即使服务器工作且输入的用户名和电子邮件有效,忘记密码功能如何显示此回显?
下面我已经包含了整个代码,所以你可以看到它的外观,如果有人想在mysqli和php中创建自己的忘记密码功能,那么你可以使用下面的代码作为模板:
以下是工作版本:
<?php
error_reporting (E_ALL ^ E_NOTICE);
session_start();
include('member.php');
include('connect.php');
$user = (isset($_POST['user'])) ? $_POST['user'] : '';
$email = (isset($_POST['email'])) ? $_POST['email'] : '';
$errormsg = (isset($errormsg)) ? $errormsg : '';
if ((!isset($username)) && (!isset($userid))){
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 = ? 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();
$pass = md5($pass);
$pass = substr($pass, 0, 15);
$pass = md5(md5("g3f".$pass."rt4"));
//update password in db
$updatesql = "UPDATE Teacher SET TeacherPassword = ? WHERE TeacherUsername = ?";
$update = $mysqli->prepare($updatesql);
$update->bind_param("ss", $pass, $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,$pass);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbTeacherUsername, $dbTeacherPassword);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
if ($numrows == 1){
$site = "http://hostie.ac.uk/u0000000/MobileDB";
$webmaster = "Mayur Patel <u0867587@hud.ac.uk>";
$headers = "From: $webmaster";
$subject = "Your New Password";
$message = "Your Password has been Reset. Your new password is below: \n";
$message .= "Password: $pass \n";
$message .= "You can Reset your Password to set your own personal Password by using the 'Reset Password' feature \n\n";
$message .= "We recommend that you store this password in a safe place and then delete this email for Security Reasons \n\n";
$message .= "Thank You";
if(mail($email, $subject, $message, $headers)){
$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";
}
}
else{
$errormsg = "An error has occured, the Password was not Reset";
}
}
else{
$errormsg = "Your Username or Email was not Correct";
}
}
else{
$errormsg = "Please enter in a Valid Email Address";
}
}
else{
$errormsg = "Please Enter in your Email";
}
}
else{
$errormsg = "Please Enter in your Username";
}
}
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' /></td>
</tr>
<tr>
<td>Email</td>
<td><input type='text' name='email' /></td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='resetbtn' value='Reset Password' /></td>
</tr>
</table>
</form>";
}
else
{
echo "Please Logout to view this Page.";
}
?>
答案 0 :(得分:0)
您的更新查询失败或您的选择查询失败。试着看看mysql是否给出错误:
printf("Error: %s.\n", $update->error);
printf("Error: %s.\n", $stmt->error);