php - 更新 SQL 中的单行

标签 php mysql sql database sql-update

我在使用一个非常简单的 SQL 语句时遇到了问题:UPDATE

我只想更新特定行中的 booking_date 列。

这是我正在使用的语句:

UPDATE `coupon-codes` SET `booking_id`=:timestamp WHERE `id` = :id

我正在使用 PDO 命名占位符。

我总是收到不正确的语法错误。我做错了什么?

编辑:

我尝试不加反引号:

UPDATE coupon-codes SET booking_id = :timestamp WHERE id = :id

还是不行。

这是我收到的错误消息:

enter image description here

编辑2:

这是我在使用反引号时收到的错误消息: enter image description here

编辑3:

作为引用,这是我之前使用过的 INSERT 语句,它运行时没有任何问题:

INSERT INTO `coupon-codes` (`code`, `date`) VALUES (:code, :date)

编辑4:

抱歉,评论中有些话说错了,为了澄清,请参阅:

我到处都使用反引号。这是不起作用的查询:

UPDATE `coupon-codes` SET `booking_date`=:timestamp WHERE `id` = :id

我在原来的问题中也有一个拼写错误,其中的 booking_id 而不是 booking_date 字段,但这并不重要,因为我收到了语法错误。

这是我尝试运行它的 PHP 代码:

$stmt = $db->prepare("UPDATE `coupon-codes` SET `booking_date`=:timestamp WHERE `id` = :id");
$stmt->bindParam(':timestamp', $time);
$stmt->bindParam(':id', $id);
$stmt->execute(); 

最佳答案

基本 MySQL 语法:

'foo' - single-quotes. turns the quote word into a string literal
`foo` - backticks. used to escape table/fieldnames that happen to be reserved words


SELECT 'select' FROM ... -- select the literal word "select" from some table
SELECT `select` FROM ... -- select the field NAMED "select" from some table
SELECT select FROM ...   -- syntax error - using a reserved word "select"

根据您的错误消息,您可能遇到以下情况之一:

UPDATE 'coupon-code' ...   -- can't update a string. must specify a table name
UPDATE coupon-code ...  -- math operation: coupon MINUS code - not a table name

关于php - 更新 SQL 中的单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26915951/

相关文章:

php - 如何获取max id之前的数据

sql - T-SQL 子查询和可能的连接的正确语法

c# - 如何将参数值放入SQL的前缀搜索中

SQL 递归计算日期

php - 在 WooCommerce 中设置特定国家/地区允许的最小重量

php - 如何利用PHP中的Execution After Redirect漏洞?

php - 如何在 WordPress 中查询多个作者的帖子?

c# - 使用c#将日期插入MySql

php - Left Join 将主键连接到普通列会造成严重破坏

php - PDO 和存储过程的无缓冲查询错误