PDO 重复更新错误
PDO on duplicate update error
我查了很多PDO的帖子,都说语法不对,可是查了好像也找不到。
这是我的代码:
$stmt = $pDatabase->prepare('INSERT INTO Agenda (index, date, shortdesc) VALUES :values ON DUPLICATE KEY UPDATE date=VALUES(date), shortdesc=VALUES(shortdesc)');
我试过在最后用 ;
修复它,或者一次插入一个。它在准备时出错,所以无论 :values
是什么都不重要。
这是产生的错误:
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 MariaDB server version for the right syntax to use near 'index, date, shortdesc) VALUES(?, ?, ?)ON DUPLICATE KEY UPDATE date=VALUES(date)' at line 1' in /customers/f/b/e/**************/httpd.www/editagenda.php:14 Stack trace: #0 /customers/f/b/e/**************/httpd.www/editagenda.php(14): PDO->prepare('INSERT INTO Age...') #1 {main} thrown in /customers/f/b/e/**************/httpd.www/editagenda.php on line 14
其中 14 是 prepare
行。
这一行在 DBadmin 中运行良好。
我的 table 看起来像这样:
index date shortdesc longdesc boolean
10 2015-12-12 Something copyshort 1
11 2015-11-12 Somethi2ng copyshort2 0
您不能像这样将一个占位符用于多个值。
来自 the manual:
Note:
Parameter markers can represent a complete data literal only. Neither part of literal, nor keyword, nor identifier, nor whatever arbitrary query part can be bound using parameters. For example, you cannot bind multiple values to a single parameter in the IN() clause of an SQL statement.
试试这个:
$stmt = $pDatabase->prepare('INSERT INTO Agenda (index, date, shortdesc) VALUES (:index, :date, :shortdesc) ON DUPLICATE KEY UPDATE date=VALUES(date), shortdesc=VALUES(shortdesc)');
(请注意,您需要 (
和 )
。)然后传递三个值,每个值对应一个占位符(:index
、:date
和 :shortdesc
).
P.S。请注意,index
和 date
在 MySQL(以及大多数 RDBMS)中是 reserved words。您需要将它们包裹在背包中,如:
$stmt = $pDatabase->prepare('INSERT INTO Agenda (`index`, `date`, shortdesc) VALUES (:index, :date, :shortdesc) ON DUPLICATE KEY UPDATE date=VALUES(`date`), shortdesc=VALUES(shortdesc)');
我查了很多PDO的帖子,都说语法不对,可是查了好像也找不到。 这是我的代码:
$stmt = $pDatabase->prepare('INSERT INTO Agenda (index, date, shortdesc) VALUES :values ON DUPLICATE KEY UPDATE date=VALUES(date), shortdesc=VALUES(shortdesc)');
我试过在最后用 ;
修复它,或者一次插入一个。它在准备时出错,所以无论 :values
是什么都不重要。
这是产生的错误:
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 MariaDB server version for the right syntax to use near 'index, date, shortdesc) VALUES(?, ?, ?)ON DUPLICATE KEY UPDATE date=VALUES(date)' at line 1' in /customers/f/b/e/**************/httpd.www/editagenda.php:14 Stack trace: #0 /customers/f/b/e/**************/httpd.www/editagenda.php(14): PDO->prepare('INSERT INTO Age...') #1 {main} thrown in /customers/f/b/e/**************/httpd.www/editagenda.php on line 14
其中 14 是 prepare
行。
这一行在 DBadmin 中运行良好。
我的 table 看起来像这样:
index date shortdesc longdesc boolean
10 2015-12-12 Something copyshort 1
11 2015-11-12 Somethi2ng copyshort2 0
您不能像这样将一个占位符用于多个值。
来自 the manual:
Note: Parameter markers can represent a complete data literal only. Neither part of literal, nor keyword, nor identifier, nor whatever arbitrary query part can be bound using parameters. For example, you cannot bind multiple values to a single parameter in the IN() clause of an SQL statement.
试试这个:
$stmt = $pDatabase->prepare('INSERT INTO Agenda (index, date, shortdesc) VALUES (:index, :date, :shortdesc) ON DUPLICATE KEY UPDATE date=VALUES(date), shortdesc=VALUES(shortdesc)');
(请注意,您需要 (
和 )
。)然后传递三个值,每个值对应一个占位符(:index
、:date
和 :shortdesc
).
P.S。请注意,index
和 date
在 MySQL(以及大多数 RDBMS)中是 reserved words。您需要将它们包裹在背包中,如:
$stmt = $pDatabase->prepare('INSERT INTO Agenda (`index`, `date`, shortdesc) VALUES (:index, :date, :shortdesc) ON DUPLICATE KEY UPDATE date=VALUES(`date`), shortdesc=VALUES(shortdesc)');