dounai9294 2019-07-12 16:36
浏览 508
已采纳

我的SQL语句是正确的但仍然返回错误

On my website I try to take a few info and add them to my database but ever since I changed the date to datetime the whole SQL statment doesn't work anymore.

I looked everywhere online and no one else seem to have the same problem I might just have missed something but the SQL statment looks correct to me.

public function addTimeLine($typeOfPost,$post,$idImage,$numberImage){
    $post = addslashes($post);
    $typeOfPost = addslashes($typeOfPost);
    $date = date("Y-m-d H:i:s");
    $query = "INSERT INTO `time_line` ( `special_note`, `type`, `id_image`, `number_image`, `dates`) VALUES ( '".$post."', '".$typeOfPost."', ".$idImage.", ".$numberImage.", '".$date."')";
    $ps = $this->_db->prepare($query);
    $ps->execute();
}

and it gives me this error

Fatal error: Uncaught PDOException: 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 ' 1, '2019-07-12 16:29:58')' at line 1

  • 写回答

2条回答 默认 最新

  • drnycqxwz63508434 2019-07-12 17:20
    关注

    Your not using prepared statements correctly. You need to use placeholders and then pass the values in the execute

    public function addTimeLine($typeOfPost, $post, $idImage, $numberImage) {
        $date = date("Y-m-d H:i:s");
        $query = "INSERT INTO `time_line` ( `special_note`, `type`, `id_image`, `number_image`, `dates`) VALUES ( ?, ?, ?, ?, NOW())";
        $ps = $this->_db->prepare($query);
        $ps->execute([
            $post,
            $typeOfPost,
            $idImage,
            $numberImage
        ]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?