PDO语法错误

0 php mysql pdo

我正在使用PDO进行项目,但我在提交时出现语法错误...这是我的代码:

<?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());
}

?>
Run Code Online (Sandbox Code Playgroud)

这就是我在浏览器中得到的:

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 )
Run Code Online (Sandbox Code Playgroud)

我想我已经忘记了一些明显但却看不出的东西......

Exp*_*lls 6

由于您已经在使用PDO,因此您应该使用带有预准备语句的参数化查询.

$stmt = $connexion->prepare("INSERT INTO article (id, idAuteur, titre, text, date)
    VALUES (0, 0, ?, ?, ?)");           
$stmt->execute(array($titre, $text, $date)); 
Run Code Online (Sandbox Code Playgroud)