PDO更新设置字段值为0?

时间:2012-10-28 20:21:40

标签: php database pdo

我尝试将旧的php sql脚本转换为PDO,但它仍然将用户点设置为0而不是添加+15

继承人原来的

<?php

session_start();
$db = mysqli_connect("host", "username", "password", "dbname"); 
$username = $_SESSION['username'];
mysqli_query($db, "UPDATE login_users SET Points=Points+15 WHERE username='$username'");


?>

这是我试图创建的PDO语句。我的语法错了吗?

<?php

session_start();
$db = new PDO('mysql:host;dbname=dbname;charset=UTF-8', 'username', 'password');
$username = $_SESSION['username'];
$Points = $db->exec("UPDATE login_users SET Points='Points+15' WHERE username='$username'");


?>

2 个答案:

答案 0 :(得分:4)

'积分+ 15'是字面意思。你需要它而没有单引号。

UPDATE login_users SET Points=Points+15 WHERE username='$username'

更好的是,利用参数化:

$Points = $db->prepare("UPDATE login_users SET Points=Points+15 WHERE username=?");
$Points->execute(array($username))

答案 1 :(得分:2)

摆脱这些引用

SET Points='Points+15' WHERE