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

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错