-3

我正在一个带有注册系统的网站上工作。在 userCP 中,您可以更改您的密码。我为它制作了这个脚本,但它不起作用。有人可以帮助我吗?当我更改密码时,它没有给出错误,但它只是没有更新它。

PHP代码:

<?php 
    if (isset($_POST['updatePassBtn']))
    {
        $cpassword = $_POST['cpassword'];
        $npassword = $_POST['npassword'];
        $rpassword = $_POST['rpassword'];
        if (!empty($cpassword) && !empty($npassword) && !empty($rpassword))
        {
            if ($npassword == $rpassword)
            {
                if (mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '".$_SESSION['username']."' AND `password` = '".SHA1($cpassword)."'")))
                {
                    mysql_query("UPDATE `users` SET `password` = '".SHA1($npassword)."' WHERE `username` = '".$_SESSION['username']."' AND `ID` = '".$_SESSION['id']."'");
                    echo '<div class="nNote nSuccess hideit"><p><strong style="color:green;">SUCCESS: </strong>Password Has Been Updated</p></div>';
                }
                else
                {
                    echo '<div class="nNote nFailure hideit"><p><strong style="color:red;">FAILURE: </strong>Current Password is incorrect.</p></div>';
                }
            }
            else
            {
                echo '<div class="nNote nFailure hideit"><p><strong style="color:red;">FAILURE: </strong>New Passwords Did Not Match.</p></div>';
            }
        }
        else
        {
            echo '<div class="nNote nFailure hideit"><p><strong style="color:red;">FAILURE: </strong>Please fill in all fields</p></div>';
        }
    }
?>

我的表格:

<form action="" class="form" method="POST">
    <fieldset>
        <label></label>
        <input name="cpassword" type="text" value="Current Password" onfocus="this.value = (this.value=='Current Password')? '' : this.value;" onblur="if(this.value == ''){this.value='Current Password';}"/>
    </fieldset>
    <fieldset>
        <label></label>
        <input name="npassword" type="text" value="New Password" onfocus="this.value = (this.value=='New Password')? '' : this.value;" onblur="if(this.value == ''){this.value='New Password';}"/>
    </fieldset>
    <fieldset>
        <label></label>
        <input name="rpassword" type="text" value="Repeat Password" onfocus="this.value = (this.value=='Repeat Password')? '' : this.value;" onblur="if(this.value == ''){this.value='Repeat Password';}"/>
        <input type="submit" value="Update" name="updatePassBtn"/>
    </fieldset>
</form>
4

2 回答 2

2

您计算与用户名和密码匹配的行数,但是当您更新时,您还具有它必须匹配 $_SESSION['id'] 的条件。如果您的会话不包含正确的“id”,那么您的更新可能不会匹配任何行。

在报告更新成功之前,您应该检查mysql_affected_rows() 。

您还应该检查 mysql 函数是否返回成功(正如@RocketHazmat 在评论中建议的那样)。许多返回false错误。

于 2013-03-14T18:53:33.283 回答
-1
 mysql_query("UPDATE `users` SET `password` = '".SHA1($npassword)."' WHERE `username` = '".$_SESSION['username']."' AND `ID` = '".$_SESSION['id']."'");

你已经设置了 mysql_query 但我没有看到你执行它

于 2013-03-14T18:52:36.203 回答