doyp9057 2013-06-11 13:43
浏览 55
已采纳

PDO INSERT语句出错

I've been staring at this statement and its error message for about half an hour without being able to see what's wrong with it.

Here is my statement:

try{
    $stmt = $conn->prepare("INSERT INTO dashboardsearchdates (userID, from, to) VALUES (?,?,?)");
    $result = $stmt->execute(array($userID, $frmFrom, $frmTo));
}
    catch(PDOException $e){
    echo 'ERROR: ' . $e->getMessage();
    $queryString = $stmt->queryString;
    $page = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    mail(ADMIN_EMAIL, 'SQL ERROR: ' . $page, 'Error Page: ' . $page . '     //     Error: ' . $e->getMessage() . '     //     Query String: ' . $queryString);
}

And this is the table structure i'm trying to insert the values into:

Table structure for table `dashboardsearchdates`

--

CREATE TABLE IF NOT EXISTS `dashboardsearchdates` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
  `userID` int(11) NOT NULL,
  `from` datetime NOT NULL,
  `to` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Here is the exception i'm seeing:

ERROR: 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 'from, to) VALUES ('9','2012-12-12','2013-01-01')' at line 1

As you can see from the exception, the three values i'm trying to insert into the DB are present at runtime (otherwise they wouldn't be shown in the exception)

Things i've tried:

I have other queries using $conn above this in the code, so i know $conn is present and working.

I've rewritten the statement as so:

$stmt = $conn->prepare("INSERT INTO dashboardsearchdates (userID, from, to) VALUES (:userID,:from,:to)");
$stmt->bindParam(':userID', $userID);
$stmt->bindParam(':from', $frmFrom);
$stmt->bindParam(':to', $frmTo);                        
$result = $stmt->execute();

I've tried backquotes around the table name, all the field names, only the integer field name, and only the date fields.. All with the same error message shown above - as far as i can see there's nothing wrong with the construction of the SQL.

I'd be really grateful for any suggestions of other things to try.

  • 写回答

2条回答 默认 最新

  • douzong5473 2013-06-11 13:45
    关注

    'from' is a key word you need to enclose it in backticks

    `from`
    

    'to' is a keyword as well

    Note: this problem has nothing to do with PDO. A developer ought to test their query in mysql client before starting for build it dynamically.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 小魔指游戏板整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?
  • ¥20 求会6sv辐射传输模型,辅导(可py6s🙏🏻有偿
  • ¥15 .xla后缀的文件拖到excel里什么内容也没有怎么办
  • ¥20 Workbench中Mechanical打不开、闪退是什么原因?