PHP PDO更新声明无效

时间:2017-04-30 03:21:01

标签: php mysql arrays pdo

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

<?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);
?>

1 个答案:

答案 0 :(得分:1)

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

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