PHP PDO 更新准备好的语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15998961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP PDO update prepared statement
提问by twigg
I'm trying to update my database with the following query:
我正在尝试使用以下查询更新我的数据库:
$sth = "UPDATE rpacks SET rpacks_location VALUES (:location) WHERE rpacks_id = (:id)";
$q = $conn->prepare($sth);
$q->execute(array(':location'=>$location, ':id'=>$id));
But I'm getting this error
但我收到这个错误
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUES ('test') WHERE rpacks_id = ('2')' at line 1' in
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUES ('test') WHERE rpacks_id = ('2')' at line 1' in
回答by Fabio
There is a mistake in your update
query because you used insert
query syntax.
您的update
查询中存在错误,因为您使用了insert
查询语法。
Here is the correct query:
这是正确的查询:
$sth = "UPDATE rpacks SET rpacks_location = :location WHERE rpacks_id = :id";
Reference:http://dev.mysql.com/doc/refman/5.0/en/update.html
回答by Nerdwood
Change to:
改成:
$sth = "UPDATE rpacks SET rpacks_location = :location WHERE rpacks_id = :id";
$sth = "UPDATE rpacks SET rpacks_location = :location WHERE rpacks_id = :id";