插入语句不起作用

时间:2014-03-06 01:24:56

标签: php mysql sql syntax-error

我总是使用普通的查询来将数据插入到数据库中,但现在我想用准备好的语句来创建它。我已经在使用语句在我的所有文件中选择数据了但是插入工作从来没有工作过...现在我再次用完了想法。也许有人可以看到我做错了什么。

$animeId        =   $_POST['animeId'];
$username   =   $_POST['username'];
$rating         =   $_POST['rating'];
$story          =   $_POST['story'];
$genre          =   $_POST['genre'];
$animation  =   $_POST['animation'];
$characters =   $_POST['characters'];
$music          =   $_POST['music'];

//Datum auslesen
$date =  date("Y-m-d H:i:s");


if($insertRating = $con->prepare("INSERT INTO anime_rating (animeId, rating, story, genre, animation, characters, music, user, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?"))
{
    $insertRating->bind_param("iiiiiiiss", $animeId, $rating, $story, $genre, $animation, $characters, $music, $username, $date);
    $insertRating->execute();
    $insertRating->close();
}

4 个答案:

答案 0 :(得分:5)

您的查询中有错误的逗号:

music, user,) VALUES (?, ?, ?, ?, ?, ?, ?                               
          ^^^
          HERE

应该是

music, user) VALUES (?, ?, ?, ?, ?, ?, ?     

答案 1 :(得分:1)

声明中:

INSERT INTO anime_rating (
 animeId, 
 rating, 
 story, 
 genre, 
 animation, 
 characters, 
 music, 
 user /* 8 columns */) 
 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?") /* 10 parameters */

列出的值列有8列,值列表中指定了10个参数。另据指出,值列表中还有额外的逗号。

列数必须与以下语句中的参数数量和绑定参数数量相匹配:

`$insertRating->bind_param("iiiiiiiss", $animeId, $rating, $story, $genre, $animation, $characters, $music, $username, $date);`

答案 2 :(得分:0)

声明中有两个错误:

INSERT INTO anime_rating (animeId, rating, story, genre, animation, characters, music, user,) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?"
                                                                                              ^ here and                       ^  ^
  1. 删除逗号
  2. 在字符串结尾之前添加一个右括号。
  3. 删除一个,?
  4. 此外,您应该从绑定中删除一个i

    $insertRating->bind_param("iiiiiiss", $animeId, $rating, $story, $genre, $animation, $characters, $music, $username, $date);
    

答案 3 :(得分:0)

if($insertRating = $con->prepare("INSERT INTO anime_rating (animeId, rating, story, genre, animation, characters, music, user, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?"))

最后一个(“)应该放在第一个之后)

新代码:

if($insertRating = $con->prepare("INSERT INTO anime_rating (animeId, rating, story, genre, animation, characters, music, user, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")