2011-06-24 86 views
1

我正试图做一个SQL查询(insert into users set cash = cash + 20), 任何人都可以帮我用PDO准备声明版本的上述查询吗?PHP - > PDO更新声明

+1

这不是一个有效的INSERT语句。这应该是“插入用户设置现金= 20”。你在寻找更新声明吗? –

+0

@Francois:哦!真。签出INSERT的第二个语法http://dev.mysql.com/doc/refman/5.5/en/insert.html –

+1

@Shakti Singh - 我欠你一个道歉。你绝对正确。我不知道你能做到这一点。 –

回答

0

你正在尝试做的更新,而不是插入

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

我真的不能弄清楚,如果你正在寻找插入或更新。这里是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));