-2

致命错误:未捕获异常 'PDOException' 并带有消息 'SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 /home/ /domains/ /public_html/new/ .php:140的“第 1 行”附近使用正确的语法堆栈跟踪:#0 /home/ /domains/ /public_html/new/ .php(140): PDOStatement->execute() #1 {main} 在 /home/ /domains/ /public_html/new/***.php 第 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');
}

我该如何解决这个问题,请帮助我:-)

非常感谢!

4

3 回答 3

4

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

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)');
于 2015-04-24T23:42:41.900 回答
3

替换这一行:

$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)');

您忘记了代码中的最后一个以及and之间)的逗号,:meid:title

于 2015-04-24T23:43:19.020 回答
1

也请接受我的建议,不要为每个 sql 命令编写循环

只需编写一次即可在任何地方运行

创建一个 php 类并只发送命令

为什么要遵循这种方法?因为你会再次遇到同样的文书错误和问题,这会让程序员发疯!

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

PHP PDO

于 2015-04-24T23:56:04.773 回答