Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP和MySQL更改登录用户密码表单,不更新密码_Php_Mysql_Pdo_Login_Warnings - Fatal编程技术网

PHP和MySQL更改登录用户密码表单,不更新密码

PHP和MySQL更改登录用户密码表单,不更新密码,php,mysql,pdo,login,warnings,Php,Mysql,Pdo,Login,Warnings,我试图允许用户在登录后在个人资料编辑页面中更新密码。我有两个问题。首先,我得到一个警告,上面写着 警告:PDOStatement::execute()最多需要1个参数,第115行需要2个参数 那是 $stmt->execute(":password",$changed_password); 我尝试了不同的语法,但这里出现了错误 第二个问题是密码实际上没有更新。下面是代码(包括如何为当前用户创建到db的连接) 上面的代码在我的文件的开头。 提交表单后会触发以下代码 <?php

我试图允许用户在登录后在个人资料编辑页面中更新密码。我有两个问题。首先,我得到一个警告,上面写着

警告:PDOStatement::execute()最多需要1个参数,第115行需要2个参数

那是

$stmt->execute(":password",$changed_password);
我尝试了不同的语法,但这里出现了错误

第二个问题是密码实际上没有更新。下面是代码(包括如何为当前用户创建到db的连接)


上面的代码在我的文件的开头。 提交表单后会触发以下代码

<?php
        if(isset($_POST['reset_password']))
        {
            $old_pass=$_POST['txtoldpassword'];
            $new_pass=$_POST['txtnewpass1'];
            $re_pass=$_POST['txtnewpass2'];

            if($row['password']==md5($old_pass)){
                if($new_pass==$re_pass){
                    $changed_password=md5($re_pass);
                    $email = $row['email'];
                    $query = $user_home->runQuery("UPDATE user SET password='$changed_password' WHERE email='$email'") or die("Could not change password at this time.");
                    $stmt->execute(":password",$changed_password);

                    ?>

                      <div class='alert alert-success alert-dismissible'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                            <strong>Password Updated Successfully</strong>
                      </div>

                <?php
                }
                else{
                    ?>

                      <div class='alert alert-danger'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                            <strong>Your new passwords do not match</strong>
                      </div>

                <?php
                }
            }
            else
            {
                ?>

                      <div class='alert alert-danger'>
                        <button class='close' data-dismiss='alert'>&times;</button>
                            <strong>Your old password is incorrect</strong>
                      </div>

            <?php
            }
        }
        ?>

您犯了几个错误:SQL注入、PDO使用不当

SQL注入 不要在查询中正确使用变量。在连接设置中使用绑定:

// wrong
$query = $user_home->runQuery("UPDATE user SET password='$changed_password' WHERE email='$email'")

// correct
$query = $user_home->runQuery("UPDATE user SET password=:password WHERE email=:email")
PDO使用不当 您正在使用绑定,但您的查询不包含要绑定的内容

使用SQL注入修复程序,您可以尝试两个选项:

// bind with specific function
$query->bindParam(':password', $changed_password);
$query->bindParam(':email', $email);

// bind in execute() function
$query->execute([
    ':password' => $changed_password,
    ':email'    => $email,
]);

阅读更多关于PDO和
execute()
函数的信息:

请阅读文档中如何使用
execute()
绑定值。您在这里使用的方法是正确的:
$stmt->execute(数组(“:uid”=>$\u会话['userSession'))
谢谢您的支持。我不知道如何投票给你的答案。我将为其他人提供更正后的代码。我已将上面的代码更改为包含Paul Spiegel和micster提供的解决方案。谢谢你们,谢谢,我改了。我得到了它。Cheers我已经修改了上面的代码,加入了Paul Spiegel和micster提供的解决方案。谢谢你们两位
// bind with specific function
$query->bindParam(':password', $changed_password);
$query->bindParam(':email', $email);

// bind in execute() function
$query->execute([
    ':password' => $changed_password,
    ':email'    => $email,
]);