我正在尝试在PHP中运行以下代码:
<?php
require_once 'Admin/Connector.php';
$name = 'A Schandillia';
$number = '12345678910';
$latitude = '73.897687';
$longitude = '11.958271';
$sql = 'INSERT INTO distilled_contacts (PHONE, POPULARNAME, PREFERREDNAME, LATITUDE, LONGITUDE, LASTSEEN) VALUES ';
$sql .= '(:num, :name, :name, :lat, :lon:, CURRENT_TIMESTAMP) ON DUPLICATE KEY UPDATE PREFERREDNAME = :name, LATITUDE = COALESCE(:lat, LATITUDE), LONGITUDE = COALESCE(:lon, LONGITUDE);';
$connect = dbconn(PROJHOST,PROJDB,PROJDBUSER,PROJDBPWD);
$query = $connect->prepare($sql);
$query->bindParam(':num', $number);
$query->bindParam(':name', $name);
$query->bindParam(':lat', $latitude);
$query->bindParam(':lon', $longitude);
$query->execute();
?>
但是,它拒绝识别CURRENT_TIMESTAMP并抛出此错误:
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 ':, "CURRENT_TIMESTAMP") ON DUPLICATE KEY UPDATE PREFERREDNAME = 'A Schandillia',' at line 1' in /Users/Amit/Sites/project/test.php:24 Stack trace: #0 /Users/Amit/Sites/project/test.php(24): PDOStatement->execute() #1 {main} thrown in /Users/Amit/Sites/project/test.php on line 24
我甚至尝试用NOW()替换它,但问题仍然存在。有没有办法以编程方式将日期时间值输入MySQL表?
P.S。请不要建议在我的桌面上使用时间戳数据类型而不是 datetime ,因为这在我的方案中不起作用。
答案 0 :(得分:1)
错误非常明确:您的SQL语法中有错误。错误甚至进入细节:错误的语法位于CURRENT_TIMESTAMP
之前的“:”字符处。
现在仔细查看您的查询:
↴
:num, :name, :name, :lat, :lon:
您在lon
的右侧看到了什么?它与lat
或name
有什么不同?