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条)

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录