PHP->;PDO更新声明


PHP -> PDO Update Statement

我试图执行SQL查询(insert into users set cash = cash + 20),有人能帮我处理上述查询的PDO准备的语句版本吗?

我真的不知道你是想插入还是更新。以下是PDO准备的语句示例。他们假设您已经连接,并且PDO对象是$dbh

插入:

$sth = $dbh->prepare('INSERT INTO `users` (`cash`) VALUES (?)');
$sth->execute(array(20));

更新:

// All users
$sth = $dbh->prepare('UPDATE `users` SET `cash` = `cash` + ?');
$sth->execute(array(20));
// A specific user (assuming that there's a field name "id")
$sth = $dbh->prepare('UPDATE `users` SET `cash` = `cash` + ? WHERE `id` = ?');
$sth->execute(array(20, $id));

您正在尝试进行更新,而不是插入

 UPDATE users SET cash = (cash + 20)
 WHERE <condition>