我在mySQL中有错误

时间:2015-04-24 23:32:37

标签: php mysql sql pdo bindparam

  

致命错误:未捕获的异常' PDOException' with message' SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,以获得在''附近使用的正确语法。在第1行'在/home//domains//public_html/new/.php:140堆栈追踪:#0 / home / / domains / / public_html / new / .php(140):PDOStatement->执行()#1 {main}抛出/ home / 第140行 / domains / / public_html / new / ***。php

下面你找到我的代码:

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

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

Thnx很多!

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

替换此行:

$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

答案 2 :(得分:0)

另外,我的建议不要为每个sql命令编写循环

只需在任何地方写一次

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

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

但是如果你准备一次sql命令,你就不需要为每个sql命令写一个。

PHP PDO