2011-06-24 108 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));