PHP更新密码脚本不起作用


PHP Update password script doesnt work

我正在开发一个带有注册系统的网站。在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>

您可以计算与用户名和密码匹配的行数,但在更新时,您还需要满足它必须与$_SESSION['id']匹配的条件。如果您的会话不包含正确的"id",则您的更新可能不匹配任何行。

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

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

 mysql_query("UPDATE `users` SET `password` = '".SHA1($npassword)."' WHERE `username` = '".$_SESSION['username']."' AND `ID` = '".$_SESSION['id']."'");

您已经设置了mysql_query,但我看不到您正在执行它