使用PDO更新表:语法错误

时间:2015-12-23 23:39:35

标签: php mysql pdo

我正在创建一个函数来更改表用户的密码,但是我遇到了语法错误

这是功能:

 public function changepassword($password, $newpassword) {
        $user_id = $_SESSION["userSession"];
        $stmt = $this->db->prepare("SELECT * FROM user WHERE user_id=:user_id");
        $stmt->execute(array(":user_id" => $user_id));
        $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($password = $userRow['password']) {
            $sql = "UPDATE user set password=:password WHERE user_id=:user_id";
            $stmt2 = $this->db->query($sql);
            $stmt->execute(array(":user_id" => $user_id, ":password" => $password ));
            $stmt2->execute();
            return true;
        } else {
            return false;
        }
    }

这是函数调用

if (isset($_POST['btn-save'])) {
    $password = $_POST['password'];
    $newpassword = $_POST['newpassword'];
    $newpassword2 = $_POST['newpassword2'];

    if ($newpassword == $newpassword2) {
        if ($user->changepassword($password, $newpassword)) {
            header("Location: selfedit.php?inserted");
        } else {
            header("Location: selfedit.php?failure");
        }
    } else {
        header("Location: selfedit.php?failurematch");
    }
}

这是我得到的错误:

  

致命错误:未捕获的异常' PDOException'与消息   ' SQLSTATE [42000]:语法错误或访问冲突:1064您有   SQL语法错误;查看与您的手册相对应的手册   MariaDB服务器版本的正确语法使用在':密码附近   WHERE user_id =:user_id'在第1行'在   C:\ xampp \ htdocs \ aDatabase2 \ class.user.php:37堆栈跟踪:#0   C:\ xampp \ htdocs \ aDatabase2 \ class.user.php(37):PDO->查询(' UPDATE用户   设置...')#1 C:\ xampp \ htdocs \ aDatabase2 \ selfedit.php(8):   USER-> changepassword(' fereira',' umdois')#2 {main}引入   第37行的C:\ xampp \ htdocs \ aDatabase2 \ class.user.php

编辑1

我根据这个问题评论改变了功能,得到了不同的错误

public function changepassword($password, $newpassword) {
    $user_id = $_SESSION["userSession"];
    $stmt = $this->db->prepare("SELECT * FROM user WHERE user_id=:user_id");
    $stmt->execute(array(":user_id" => $user_id));
    $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($password == $userRow['password']) {
        $sql = "UPDATE user set password=:newpassword WHERE user_id=:user_id";
        $stmt2 = $this->db->prepare($sql);
        $stmt2->execute(array(":user_id" => $user_id, ":password" => $newpassword ));
        return true;
    } else {
        return false;
    }
}

新错误:

  

致命错误:未捕获的异常' PDOException'与消息   ' SQLSTATE [HY093]:参数号无效:参数未定义'   在C:\ xampp \ htdocs \ aDatabase2 \ class.user.php中:37堆栈跟踪:#0   C:\ XAMPP \ htdocs中\ aDatabase2 \ class.user.php(37):   PDOStatement->执行(数组)#1   C:\ XAMPP \ htdocs中\ aDatabase2 \ selfedit.php(8):   USER-> changepassword(' ferreira',' anotherpass')#2 {main}引入   第37行的C:\ xampp \ htdocs \ aDatabase2 \ class.user.php

1 个答案:

答案 0 :(得分:1)

它的工作方式是从编辑1 更改此行:

 $sql = "UPDATE user set password=:newpassword WHERE user_id=:user_id";

对此:

$sql = "UPDATE user set password=:newpassword WHERE user_id=:user_id";

最终功能:

public function changepassword($password, $newpassword) {
    $user_id = $_SESSION["userSession"];
    $stmt = $this->db->prepare("SELECT * FROM user WHERE user_id=:user_id");
    $stmt->execute(array(":user_id" => $user_id));
    $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($password == $userRow['password']) {
        $sql = "UPDATE user set password=:password WHERE user_id=:user_id";
        $stmt2 = $this->db->prepare($sql);
        $stmt2->execute(array(":user_id" => $user_id, ":password" => $newpassword ));
        return true;
    } else {
        return false;
    }
}