<?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">
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 );
?>
这样你吧更新语句放外面,无论什么密码都会更新啊= = 还有 mysql不要用了尽量使用mysqli吧