简体   繁体   中英

Invalid PDO MySQL Statement - Error in Syntax

So, I have the following PDO statement, which is one of several similarly constructed statements on the page. The rest are working fine, so I am confused as to why this one is stating invalid syntax.

$test_id = '1';
$option = 'a';
$ab_email = 'test@testing.com';

$stmt = $pdo_db->prepare("INSERT INTO `ab_tests_visitors` (test_id,option,visits,email) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE email=?");
$stmt->execute(array($test_id,$option,1,$ab_email,$ab_email));

The schema for the database has 5 columns, 2 of which are indexes.

  • visitor_id is int(10) and auto_increment, and also primary
  • test_id is int(10)
  • option is varchar(100)
  • visits is int(10) and default of 1
  • email is varchar(255) and unique

The error being given is:

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 'option,visits,email) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE email=?' at line 1'

option is a MySQL keyword . If you want to use it as an identifier, make sure to always surround it with backticks (as you should do with identifiers anyway):

$stmt = $pdo_db->prepare("INSERT INTO `ab_tests_visitors` (`test_id`,`option`,`visits`,`email`) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE `email`=?");

对于这个问题,在你列的名称,必须使用反引号`周围的列名(ALT + 96)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM