我在 mySQL 中有错误

I have an error in mySQL

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 '' at line 1' in /home//domains//public_html/new/.php:140 Stack trace: #0 /home//domains//public_html/new/.php(140): PDOStatement->execute() #1 {main} thrown in /home//domains//public_html/new/***.php on line 140

下面是我的代码:

if(count($errors) == 0) {
    $title = trim($_POST['title']);
    $category = trim($_POST['category']);
    $ing = trim($_POST['ing']);
    $ing_pond = trim($_POST['ing_pond']);
    $ing_note = trim($_POST['ing_note']);
    $description = trim($_POST['description']);
    $time = trim($_POST['time']);
    $insert = $dbh->prepare('INSERT INTO recettes (id, id_user, title, category, ing, ing_pond, ing_note, description, graad, time) VALUES (NULL, :meid :title, :category, :ing, :ing_pond, :ing_note, :description, :graad, :time');
    $insert->bindParam(':meid', $me['id'], PDO::PARAM_INT);
    $insert->bindParam(':title', $title, PDO::PARAM_STR);
    $insert->bindParam(':category', $category, PDO::PARAM_STR);
    $insert->bindParam(':ing', $ing, PDO::PARAM_STR);
    $insert->bindParam(':ing_pond', $ing_pond, PDO::PARAM_STR);
    $insert->bindParam(':ing_note', $ing_note, PDO::PARAM_STR);
    $insert->bindParam(':description', $description, PDO::PARAM_STR);
    $insert->bindParam(':graad', $graad, PDO::PARAM_STR);
    $insert->bindParam(':time', $time, PDO::PARAM_STR);
    $insert->execute();
    $uid = $dbh->lastInsertId();
    echo alert('Uw gerecht is succesvol toegevoegd.', 'success');
    $added = true;
}else{
    echo alert('Er ging wat mis. De volgende dingen gingen fout:<ul><li>' . join('</li><li>', $errors) . '</li></ul>Het gerecht is helaas niet toegevoegd.', 'danger');
}

我该如何解决这个问题,有人可以帮助我:-)

非常感谢!

在您的查询中,查看这一行:

VALUES (NULL, :meid :title, :category, :ing, :ing_pond, :ing_note, :description, :graad, :time');

注意:meid:title

之间没有逗号

这应该是

VALUES (NULL, :meid, :title, :category, :ing, :ing_pond, :ing_note, :description, :graad, :time)');

替换此行:

$insert = $dbh->prepare('INSERT INTO recettes (id, id_user, title, category, ing, ing_pond, ing_note, description, graad, time) VALUES (NULL, :meid :title, :category, :ing, :ing_pond, :ing_note, :description, :graad, :time');

这一行:

$insert = $dbh->prepare('INSERT INTO recettes (id, id_user, title, category, ing, ing_pond, ing_note, description, graad, time) VALUES (NULL, :meid, :title, :category, :ing, :ing_pond, :ing_note, :description, :graad, :time)');

您忘记了代码中的最后一个 ):meid:title

之间的逗号 ,

也听取我的建议,不要为每个 sql 命令编写循环

只需写一次运行任何地方

创建一个 php class 并仅发送命令

为什么你应该遵循这个方法? 因为你会再次遇到同样的笔误和问题,这会让程序员抓狂!

但是,如果您准备一次 sql 命令,则无需为每个 sql 推荐编写。

PHP PDO