【问题标题】:PDO Exception ErrorPDO 异常错误
【发布时间】:2017-03-31 13:00:04
【问题描述】:

您好,我收到 PDO 异常,我做错了吗?我尝试使用相同的查询在 PhpMyAdmin 中插入数据,但它确实有效。

错误:类型:PDOException 代码:42000

消息:SQLSTATE[42000]:语法错误或访问冲突:1064 You 您的 SQL 语法有错误;检查对应的手册 您的 MariaDB 服务器版本,以便在 '' 附近使用正确的语法 第 1 行 文件:X:\SproutTech\Fashion-Blog\cms\app\Models\Post.php 行: 36

try {

    $statement = $this->connection->prepare("INSERT INTO posts(title,content,image_path,tags,author,status,cat_id) VALUES(:title,:content,:image_path,:tags,:author,:status,:cat_id");

        $statement->execute(array(  ":title"=>$array['title'],
                                    ":author"=>$array['author'],
                                    ":content"=> $array['content'],
                                    ":image_path"=>$array['image_path'],
                                    ":tags"=>$array['tags'],
                                    ":status"=>$array['status'],    
                                    ":cat_id"=>$array['cat_id'], ));

} catch (PDOException $e) {

    echo $e->getMessage();

}

【问题讨论】:

  • ":cat_id"=>$array['cat_id'] 之后多了一个,。删除它,休息对我来说看起来不错。也尝试一次$statement->execute(array(":title"=>$array['title'],":content"=> $array['content'], ":image_path"=>$array['image_path'],":author"=>$array['author'],":status"=>$array['status'],":tags"=>$array['tags'],":cat_id"=>$array['cat_id'])); (我不认为顺序变化是这里的问题,但这是将事情按确切顺序排列的正确方法)

标签: php mysql pdo


【解决方案1】:

一个) 被遗漏了," 在你的第一行代码中被放错了位置,所以像下面这样改变它:-

$statement = $this->connection->prepare("INSERT INTO posts(title,content,image_path,tags,author,status,cat_id) VALUES(:title,:content,:image_path,:tags,:author,:status,:cat_id)"); 

check ) 并在最后引用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 2012-11-18
    • 2020-09-23
    • 2012-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多