我在使用一个非常简单的 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
还是不行。
这是我收到的错误消息:
编辑2:
这是我在使用反引号时收到的错误消息:
编辑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/