代码之家  ›  专栏  ›  技术社区  ›  KPM

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

  •  -2
  • KPM  · 技术社区  · 7 年前

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

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

    这是

    $stmt->execute(":password",$changed_password);
    

    我尝试了不同的语法,但这里出现了错误。

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

    <?php
      session_start();
      require_once 'class.user.php';
      $user_home = new USER();
      $msg = '';
    
      if(!$user_home->is_logged_in())
      {
         $user_home->redirect('signin.php');
      }
    
      $stmt = $user_home->runQuery("SELECT * FROM user WHERE id=:uid");
      $stmt->execute(array(":uid"=>$_SESSION['userSession']));
      $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
      // print_r($row['learner_type']);
      ?>
    

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

    <?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
                }
            }
            ?>
    

    谢谢你的帮助。注意,我已经检查了$email=$行['email'];并返回当前登录的用户。

    1 回复  |  直到 7 年前
        1
  •  1
  •   micster    7 年前

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

    SQL注入

    不要在查询中正确使用变量。在connexion设置中使用绑定:

    // 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() 此处的功能: https://secure.php.net/manual/en/pdostatement.execute.php