1

我正在尝试从登录的用户更改密码。在页面上它说密码已更改,但它没有更改,不在数据库上,也没有更改。我尝试使用新密码登录,但只有旧密码才能登录。我确实刷新了页面和所有内容。我尝试了几天,我想知道是否有人会发现我的错误。今晚我以为可能是因为密码在数据库上用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>`
4

1 回答 1

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) 会话必须在 key 下设置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');
于 2013-01-06T23:47:51.400 回答