<?php session_start (); $username = $_REQUEST ["username"]; $oldpassword = $_REQUEST ["oldpassword"]; $newpassword = $_REQUEST ["newpassword"]; $con = mysql_connect ( "localhost", "root", "root" ); if (! $con) { die ( '数据库连接失败' . $mysql_error () ); } mysql_select_db("mtdb",$con); $dbusername = null; $dbpassword = null; $result = mysql_query ( "select * from t_account where username ='{$username}'" ); while ( $row = mysql_fetch_array ( $result ) ) { $dbusername = $row ["username"]; $dbpassword = $row ["password"]; } if (is_null ( $dbusername )) { ?> <script type="text/javascript"> alert("用户名不存在"); window.location.href="changepasswd.html"; </script> <?php } if ($oldpassword!=$dbpassword) { ?> <script type="text/javascript"> alert("密码错误"); window.location.href="changepasswd.html"; </script> <?php } mysql_query ( "update t_account set password='{$newpassword}' where username='{$username}'" ) or die ( "存入数据库失败" . mysql_error () );//如果上述用户名密码判定不错,则update进数据库中 mysql_close ( $con ); ?> <script type="text/javascript"> alert("密码修改成功"); window.location.href="changepasswd.html"; </script>
程式碼是依照http://blog.csdn.net/qazcxh/article/details/45726911這裡寫的,現在的問題是,輸入錯誤的密碼後,會提示密碼錯誤,但是資料庫會更新新的密碼,請教各位怎麼修改
段旭涛2018-03-07 11:04:41
php程式在顯示內容之後,還是會繼續執行,一直到碰到die()、exit()之類的終止或一直到執行完。
所以處理你的問題有兩種方法
1.判斷之後加上die
2.把判斷之後的程式碼放到else裡面
662018-03-06 16:39:58
if ($oldpassword!=$dbpassword) {
?>
<script type="text/javascript"> 錯誤");
window.location.href="changepasswd.html";
</script>
# <?php
#mysql_query ( "update t_account set password='{$newpassword}' where username='{$username}'" ) or die ( "存入資料庫失敗" . mysql_err update進資料庫中
}
#
mysql_close ( $con );
## ?>##r? #