PHP SQL insert语句返回语法错误,不确定它可能是什么

时间:2012-08-03 15:05:09

标签: php mysql mysqli

我目前正在关注PHP教程,并且我已经获得以下代码::

$sql = "insert into practice (title, synopsis, genre, release, score, poster) 
        values('{$title}','{$synopsis}','{$genre}','{$release}','{$score}','{$poster}')";

if ($result = $mysqli->query($sql)) {
    // movie successfully added
    // redirect to index.php
    header("Location: index.html");
    exit;
}
elseif ($mysqli->connect_errno) {
    // there was a database error when inserting
    printf("Insert failed: %s\n", $mysqli->error);
}
else {
    printf("Sorry this isn't working! Insert failed: %s\n", $mysqli->error);
}

我目前收到错误消息,指出"抱歉这不起作用......" mysqli错误只是说明:

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 'release, score, poster) values ('fa','f','ff','ff','ff','ff')' at line 1.

我是一个完整的新手,所以我在语法错误实际上可能会有点丢失。感谢您提前提供任何帮助

3 个答案:

答案 0 :(得分:8)

releaseMySql 5.1

中的保留关键字

你需要用反引号封装

$sql = "insert into practice (title, synopsis, genre, `release`, score, poster)  
        values('{$title}','{$synopsis}','{$genre}','{$release}','{$score}','{$poster}')"; 

答案 1 :(得分:2)

“release”似乎是mysql的关键字。

尝试用“`”转义关键词。

$sql = "insert into practice (title, synopsis, genre, `release`, score, poster) 
    values('{$title}','{$synopsis}','{$genre}','{$release}','{$score}','{$poster}')";

答案 2 :(得分:2)

它说语法问题从“发布”开始。由于它看起来不像语法错误,它可能是一个保留字。请参阅http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html