PHP-> PDO更新声明

发布于 2024-11-17 05:36:06 字数 99 浏览 1 评论 0原文

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

I was trying to do a SQL query ( insert into users set cash = cash + 20 ),
can anyone help me with the PDO prepared statement version of the above query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笛声青案梦长安 2024-11-24 05:36:06

我真的不知道你是要插入还是更新。以下是 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));

I can't really figure out if you're looking to insert or update. Here are PDO prepared statement examples. They assume that you've already connected and that the PDO object is $dbh.

Insert:

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

Update:

// 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));
心清如水 2024-11-24 05:36:06

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

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

You are trying to do an update, not an insert

 UPDATE users SET cash = (cash + 20)
 WHERE <condition>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文