doudi7782 2018-04-26 19:20
浏览 47

changePSW功能不起作用

can you help out a beginner trying to learn PHP? I wrote a code for changing password without any validations yet, just to change it and it does not work. It's been days I've been trying and couldn't figure out what's wrong. Thanks in advance. id is variable name in database where id is kept. db connection is done with first line and it definitely works.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
print_r($_SESSION);


function changePSW()
{
    //$password = $_POST['currPassword']; // required
    $newPassword = $_POST['newPassword']; // required
    //$newPassword2 = $_POST['NewPassword2']; // required
    $newPasswordH = password_hash($newPassword, PASSWORD_DEFAULT);
    echo($newPassword);

    $id = $_SESSION['userID'];
    echo($id);
    // create PDO connection object
    $dbConn = new DatabaseConnection();
    $pdo = $dbConn->getConnection();

    try {
        $statement = $pdo->prepare("SELECT * FROM `users` WHERE id = :id LIMIT 1");
        $statement->bindParam(':id', $id);
        $statement->execute();

        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
        echo "SADASDASD";

        // no user matching the email
        if (empty($result)) {
            $_SESSION['error_message'] = 'Couldnt find user';
            header('Location: /Online-store/userForm.php');
            return;
        }
        $sql = "UPDATE users SET password=:newPasswordH WHERE id = :id";

        // Prepare statement
        $stmt = $pdo->prepare($sql);
        echo "AFGHANIKO";
        // execute the query
        $update_status = $stmt->execute(array(':password' => $newPasswordH, ':id' => $id));
        echo "IHAAA";
        echo($update_status);
        if ($update_status === TRUE) {
            echo("Record updated successfully" . "
");
            echo nl2br("
Password: ");
            echo ($newPassword);
            echo nl2br("
Hashed Password: ");
            echo ($newPasswordH);
            return true;
        } else {
            echo "Error updating record";
            die();

        }

    } catch (PDOException $e) {
        // usually this error is logged in application log and we should return an error message that's meaninful to user
        return $e->getMessage();
    }
}


if($_SESSION['isLoggedIn'] == true) {

    require_once("database/DatabaseConnection.php");

    unset($_SESSION['success_message']);
    unset($_SESSION['error_message']);

    changePSW();

}
?>
  • 写回答

1条回答 默认 最新

  • duansanzi5265 2018-04-26 21:12
    关注
    $update_status = $stmt->execute(array(':newPasswordH' => $newPasswordH, ':id' => $id));
    

    This is what I needed to have instead of

    $update_status = $stmt->execute(array(':password' => $newPasswordH, ':id' => $id));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图