Reputation: 3
I'm using PDO for a project but i have a syntax error on submit... here is my code :
<?php
require_once('_database.php');
$titre = $_POST['titre'];
$text = $_POST['text'];
$date = date("Y-m-d");
try
{
// Insertion dans la base de donnée
$requete = $connexion->exec("INSERT INTO article (id, idAuteur, titre, text, date) VALUES (0, 0, $titre, $text, $date)");
if (!$requete) {
echo "\nPDO::errorInfo():\n";
print_r($connexion->errorInfo());
echo $requete;
}
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
?>
And this is what I got in my browser:
PDO::errorInfo(): Array ( [0] => 42000 [1] => 1064 [2] => 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 '
Your contentrgrgrg
, 2013-12-18)' at line 1 )
I suppose I've forgotten something obvious but can't see what...
Upvotes: 0
Views: 99
Reputation: 191749
Since you're already using PDO, you should be using parameterized queries with prepared statements.
$stmt = $connexion->prepare("INSERT INTO article (id, idAuteur, titre, text, date)
VALUES (0, 0, ?, ?, ?)");
$stmt->execute(array($titre, $text, $date));
Upvotes: 6