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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:20:15  来源:igfitidea点击:

PHP PDO update prepared statement

phppdo

提问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 updatequery because you used insertquery 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

参考: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";