我在一个项目中使用 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());
}
?>
这是我在浏览器中得到的:
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 )
我想我忘记了一些明显的事情,但看不到什么......
最佳答案
由于您已经在使用 PDO,因此您应该使用 prepared statements 的参数化查询.
$stmt = $connexion->prepare("INSERT INTO article (id, idAuteur, titre, text, date)
VALUES (0, 0, ?, ?, ?)");
$stmt->execute(array($titre, $text, $date));
关于php - PDO语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44246373/