在PHP和mySQL中更改用户密码 [英] Changing User Password in PHP and mySQL

查看:87
本文介绍了在PHP和mySQL中更改用户密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过长时间的休息后才找到答案!

Found the answer after a long break from looking at it!

只是简单地更改

$querynewpass = "UPDATE tz_members SET `pass`='".$_POST['$passwordnew1']."' WHERE usr='{$_SESSION['usr']}'";

收件人:

$querynewpass = "UPDATE tz_members SET `pass`='".md5($_POST['passwordnew1'])."' WHERE usr='{$_SESSION['usr']}'";

只是我错过的简单md5!

just the simple md5 that i had missed off!

im试图使用一种形式来更改用户密码,在该表单中,用户输入当前密码和新密码.它应检查mySQL数据库以查看其输入的当前密码是否与他们登录的当前会话用户匹配,然后将mySQL数据库更新为新密码.这是到目前为止我拥有的脚本:

im trying to change a user password using a form where they enter their current password and a new password. it should check the mySQL database to see if their current password that was entered matches the current session user they are logged into and then update the mySQL database to the new password. Here is the script i have for it so far:

if($_POST['submit']=='Change')
{
    // Checking whether the Password Change form has been submitted

    $err = array();
    // Will hold our errors


    if(!$_POST['password1'] || !$_POST['passwordnew1'])
        $err[] = 'All the fields must be filled in!';

    if(!count($err))
    {
        $_POST['password1'] = mysql_real_escape_string($_POST['password1']);
        $_POST['passwordnew1'] = mysql_real_escape_string($_POST['passwordnew1']);

        // Escaping all input data

        $row = mysql_fetch_assoc(mysql_query("SELECT id,usr FROM tz_members WHERE usr='{$_SESSION['usr']}' AND pass='".md5($_POST['password1'])."'"));

        if($row['usr'])
        {
            // If everything is OK change password

            $querynewpass = "UPDATE user SET `password`='".$_POST['$passwordnew1']."' WHERE id='".$_SESSION['usr']."'";
                        $resultnewpass = mysql_query($querynewpass) or die(mysql_error()); 

        }
        else $err[]='Wrong Password To Start With!';
    }

    if($err)
    $_SESSION['msg']['passwordchange-err'] = implode('<br />',$err);
    // Save the error messages in the session

    header("Location: index.php");
    exit;
}

但它带有错误表'databasename.user'不存在" 我有一个登录和注册表格,可以使用此方法正常工作!

but it comes with an error "Table 'databasename.user' doesn't exist" i have a login and register form that work using this method without error!

更新: 我有一个数据库,该数据库具有一个名为tz_members的表,其列为id,pass,user,regIP和dt

UPDATE: i have a database that has a table called tz_members and the columns are id, pass, user, regIP and dt

我的mysql查询现在是:

my mysql query is now:

$querynewpass = "UPDATE tz_members SET `pass`='".$_POST['$passwordnew1']."' WHERE usr='{$_SESSION['usr']}'";

再次更新 添加表单代码供您查看:

UPDATED AGAIN adding the form code for you to see:

    <!-- Pass Change Form -->
            <form action="" method="post">      
            <?php

                    if($_SESSION['msg']['passwordchange-err'])
                    {
                        echo '<div class="err">'.$_SESSION['msg']['passwordchange-err'].'</div>';
                        unset($_SESSION['msg']['passwordchange-err']);
                    }

                    if($_SESSION['msg']['passwordchange-success'])
                    {
                        echo '<div class="success">'.$_SESSION['msg']['passwordchange-success'].'</div>';
                        unset($_SESSION['msg']['passwordchange-success']);
                    }
                ?>

                <label class="grey" for="password1">Current Password:</label>
                <input class="field" type="password" name="password1" id="password1" value="" size="23" />
                <label class="grey" for="password">New Password:</label>
                <input class="field" type="password" name="passwordnew1" id="passwordnew1" size="23" />
                <input type="submit" name="submit" value="Change" class="bt_register" />
            </form>

推荐答案

您正在两个似乎与同一个表相关的查询中使用两个不同的表(和列).

Your are using two different tables (and columns) in the two queries that seem to be related to the same table.

检查您的架构并适当调整第二个查询.

Check your schema and adjust the second query appropriately.

第二个查询应该类似于以下内容:

The second query should probably be something along the lines of:

$querynewpass = "UPDATE tz_members SET `pass`='".md5($_POST['passwordnew1'])."' WHERE usr='{$_SESSION['usr']}'";

这篇关于在PHP和mySQL中更改用户密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆