PHP PDO更新语句不起作用 [英] PHP PDO Update Statement not working

查看:278
本文介绍了PHP PDO更新语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对天才来说可能是一件容易的事,但是我已经尝试了所有我知道的方法并且无法使此UPDATE语句起作用.问题在于更新语句或执行绑定.我希望该语句将2点添加到用户的点列中.

This is probably an easy thing for you geniuses, but I have tried all of the ways I know and can't get this UPDATE Statement to work. The problem is with the update statement or the execute binding. I want the statement to add 2 points to the user's points column.

<?php
$dbConnection = new PDO('mysql:dbname=App;host=localhost;charset=utf8', '*', '*');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$points = 2;
$username = $_POST["username"];
$password = $_POST["password"];
$response = array();
$stmt->$dbConnection->prepare("UPDATE user SET points = points + ? WHERE username = ? AND password = ?");
$stmt->execute(array($points, $username, $password));
$hi = $dbConnection->prepare("SELECT username, password, points FROM user WHERE username = ? AND password = ?");
$hi->execute(array($username, $password));
$red = $hi->fetchAll(PDO::FETCH_ASSOC);
if (count($red) > 0){
    $response["success"] = true;
    foreach($red as $item) {
        $response["username"] = $item["username"];
        $response["password"] = $item["password"];
        $asd = $item["points"];
        $response["points"] = (string)$asd;
    }
}else{
    $response["success"] = false;
}
    echo json_encode($response);
?>

推荐答案

您需要将$ stmt分配给来自连接的准备好的语句

You need to assign $stmt to the prepared statement from the connection

$stmt = $dbConnection->prepare("UPDATE user SET points = points + ? WHERE username = ? AND password = ?");

这篇关于PHP PDO更新语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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