PHP develops simple book background management system password change function
We completed the administrator password modification page in the previous section
This section will implement this function
<script type="text/javascript"> function checkspace(checkstr) { var str = ''; for(i = 0; i < checkstr.length; i++) { str = str + ' '; } return (str == checkstr); } function check() { if(checkspace(document.renpassword.password.value)) { document.renpassword.password.focus(); alert("原密碼不能為空!"); return false; } if(checkspace(document.renpassword.password1.value)) { document.renpassword.password1.focus(); alert("新密碼不能為空!"); return false; } if(checkspace(document.renpassword.password2.value)) { document.renpassword.password2.focus(); alert("確認密碼不能為空!"); return false; } if(document.renpassword.password1.value != document.renpassword.password2.value) { document.renpassword.password1.focus(); document.renpassword.password1.value = ''; document.renpassword.password2.value = ''; alert("新密碼和確認密碼不相同,請重新輸入"); return false; } document.admininfo.submit(); } </script>Use the database SQL statement to query whether the original password entered matches the password filled in the text box If the match is successful, the modification function of the SQL statement will be used to modify the password in the database After the password is successfully modified, return to the login page and log in again using the new password.
<?php $password=$_SESSION["pwd"]; $sql="select * from admin where password='$password'"; $rs=mysqli_query($link,$sql); $rows=mysqli_fetch_assoc($rs); $submit = isset($_POST["Submit"])?$_POST["Submit"]:""; if($submit) { if($rows["password"]==$_POST["password"]) { $password2=$_POST["password2"]; $sql="update admin set password='$password2' where id=1"; mysqli_query($link,$sql); echo "<script>alert('修改成功,請重新進行登陸!');window.location='login.php'</script>"; exit(); } else ?> <?php { ?> <script> alert("原始密碼不正確,請重新輸入") location.href="renpassword.php"; </script> <?php } } ?>