ryan ryan - 16 days ago 5
MySQL Question

PDO Exception Error

Hi I am getting PDO Exception, am i doing something wrong ? I tried to use the same query to insert data in PhpMyAdmin and it works though.

Error :Type: PDOException
Code: 42000


Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You
have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use near '' at
line 1 File: X:\SproutTech\Fashion-Blog\cms\app\Models\Post.php Line:
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();

}

Answer

Issue that seems to me:-

1.One extra , after ":cat_id"=>$array['cat_id']

So remove it.

2.One ) is missed and " is misplaced in your first code line, so change it like below:-

$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 ) and quote in last

So complete code should be:-

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'],":content"=> $array['content'], ":image_path"=>$array['image_path'],":author"=>$array['author'],":status"=>$array['status'],":tags"=>$array['tags'],":cat_id"=>$array['cat_id'])); // maintain the sequence for good coding practic and cut the ambiguity

} catch (PDOException $e) {

    echo $e->getMessage();

}
Comments