我正在尝试在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表中输入datetime值?

附言请不要建议在我的表上使用时间戳数据类型代替日期时间,因为这在我的方案中不起作用。

最佳答案

该错误非常明显:您的SQL语法有错误。错误甚至涉及到细节:错误的语法位于CURRENT_TIMESTAMP之前的“:”字符处。

现在仔细查看您的查询:

                              ↴
:num, :name, :name, :lat, :lon:


您在lon右侧看到什么?它与latname有什么不同吗?

10-01 23:21