PHP功能错误不更新密码但收到确认消息

时间:2013-01-06 23:39:27

标签: php html mysql

我正在尝试更改登录用户的密码。在页面上显示密码已更改,但不会更改,不会更改数据库,也不会更改。我尝试使用新密码登录,但只有旧密码仍然可以登录。我刷新了页面和所有内容。我正在尝试这几天,我想知道是否有人会发现我的错误。今晚我认为可能是因为密码是用数据库上的md5加密的。我将如何继续,因为我已经尝试过一些事情并且不起作用。

error_reporting(E_ALL); ini_set("display_errors","On");
<?php include "includes/connection.php" ?>
<?php
session_start();

if(@$_REQUEST["Submit"]=="Update")
{
$sql="update users set password ='$_REQUEST[newpassword]' where      user='$_SESSION[myusername]'";
if (!mysql_query($sql)) die('err: PROBLEM IN QUERY: '.mysql_error());
header("Location:changpass.php?msg=updated");
}
else
die('err: PROBLEM IN REQUEST');

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Change password</TITLE>
<script language="javascript" type="text/javascript">
function validate()
{

var formName=document.frm;

if(formName.newpassword.value == "")
{
document.getElementById("newpassword_label").innerHTML='Please Enter New Password';
formName.newpassword.focus();
return false;
}
else
{
document.getElementById("newpassword_label").innerHTML='';
}


if(formName.cpassword.value == "")
{
document.getElementById("cpassword_label").innerHTML='Enter ConfirmPassword';
formName.cpassword.focus();
return false;
}
else
{
document.getElementById("cpassword_label").innerHTML='';
}


if(formName.newpassword.value != formName.cpassword.value)
{
document.getElementById("cpassword_label").innerHTML='Passwords Missmatch';
formName.cpassword.focus()
return false;
}
else
{
document.getElementById("cpassword_label").innerHTML='';
}
}
</script>
<style type="text/css">
<!--
.style1 {font-weight: bold}
.style7 {
color: yellow;
font-size: 24px;
}
.style9 {
color: #FF6666;
font-weight: bold;
}
.style12 {
color: #666666;
font-weight: bold;
}
.style14 {color: #CC0033; font-weight: bold; }
-->
</style>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
</HEAD>
<BODY>

<form action="changpass.php" method="post" name="frm" id="frm" onSubmit="return   validate();">
<table width="47%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr bgcolor="#666666">
<td colspan="2"><span class="style7">Change Password</span></td>
</tr>
<?php if(isset($_REQUEST['msg']) && $_REQUEST['msg'] == 'updated') { ?>
<tr bgcolor="#666666">
<td colspan="2"><span class="style7">Password has been changed successfully.</span></td>
</tr>
<?php } ?>
<tr>
<td bgcolor="#CCCCCC"><span class="style14">New Password:</span></td>
<td bgcolor="#CCCCCC"><input type="password" name="newpassword" id="newpassword"    size="20" autocomplete="off"/>&nbsp; <label id="newpassword_label" 

class="level_msg"></td>
</tr>
<tr> 
<td bgcolor="#CCCCCC"><span class="style14">Confirm Password:</span></td>
<td bgcolor="#CCCCCC"><input type="password" name="cpassword" id="cpassword" size="20"  autocomplete="off">&nbsp; <label id="cpassword_label" 

class="level_msg"></td>
</tr><tr bgcolor="#666666"><td colspan="2" align="center"><input type="submit"   name="Submit" value="Update" /></td>
</tr></table><a href="index.php">Home</a></form></BODY></HTML>`

1 个答案:

答案 0 :(得分:2)

我增强了你的代码:

<?php
    if ($_REQUEST['Submit'] == "Update")
    {
        $sql = "UPDATE `users` SET `password`='".$_REQUEST['newpassword']."' WHERE `user`='".$_SESSION['myusername']."'";
        mysql_query($sql);
        header("Location: changpass.php?msg=updated");
        exit;
    }
?>

要使用此代码,请确保完成以下操作:

1)您有正确的提交按钮,示例如下:

  

<input type="submit" name="Submit" value="Update" />

2)您有正确的密码字段,例如:

  

<input type="password" name="newpassword" value="" />

3)必须在密钥myusername

下设置会话

4)确保您的表字段和名称没问题。做更多的调试。

如果仍然无法解决,请添加表单代码以供进一步调查。

修改

以下是对此代码的解释,其中包含一些可能有用的基本调试目的:

<?php
    if (isset($_REQUEST['Submit']) && $_REQUEST['Submit'] == "Update")
    {
        if (!isset($_SESSION['myusername'])) trigger_error("DEBUG: SESSION VARIABLE IS NOT SET");
        if (!isset($_REQUEST['newpassword']) || empty($_REQUEST['newpassword'])) trigger_error("DEBUG: NEW PASSWORD IS NOT GIVEN");

        $sql = "UPDATE `users` SET `password`='".mysql_real_escape_string($_REQUEST['newpassword'])."' WHERE `user`='".mysql_real_escape_string($_SESSION['myusername'])."'";
        $qw = mysql_query($sql);
        if (!$qw) trigger_error("DEBUG: MYSQL RESPONDED WITH ERROR - ".mysql_error());

        header("Location: changpass.php?msg=updated");
        exit;
    }
?>

更新:调试步骤#1

将您的php文件中的代码更改为此,并告诉我们是否显示任何错误消息。

if(@$_REQUEST["Submit"]=="Update")
{
    $sql="update users set password ='$_REQUEST[newpassword]' where     user='$_SESSION[myusername]'";
    if (!mysql_query($sql)) die('err: PROBLEM IN QUERY: '.mysql_error());
    header("Location:changpass.php?msg=updated");
}
else
    die('err: PROBLEM IN REQUEST');