简体   繁体   English

插入语句不起作用

[英]Insert Statement won't work

I was always using normal querys for inserting data into the database but now I want to make it with prepared statements. 我一直在使用普通查询将数据插入数据库,但是现在我想使用准备好的语句进行查询。 I'm already using statements to select data in all my files but insert never worked... And now I ran out of ideas again. 我已经在使用语句来选择所有文件中的数据,但是插入从未起作用...现在,我再次没有想法。 Maybe someone can see what I did wrong. 也许有人可以看到我做错了。

$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();
}

You have an errant comma in your query: 您的查询中有一个逗号错误:

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

It should be 它应该是

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

In the statement: 在声明中:

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

There are 8 columns listed to insert values into and 10 parameters specified in the values section. 列出了8列以将值插入其中,并在values部分中指定了10个参数。 Also as pointed out there is the extra comma in the list of values. 还要指出的是,值列表中还有多余的逗号。

The number of columns must match the number of parameters and the number of parameters binding in the following statement: 列数必须与以下语句中的参数数和绑定的参数数匹配:

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

Two errors in the statement: 语句中的两个错误:

INSERT INTO anime_rating (animeId, rating, story, genre, animation, characters, music, user,) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?"
                                                                                              ^ here and                       ^  ^
  1. remove the comma 删除逗号
  2. add a closing parentheses before the end of the string. 在字符串末尾添加一个右括号。
  3. remove one ,? 删除一个,?

Furthermore you should chop one i s from the binding: 此外,您应该从绑定中砍掉一个i

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

The last (") should be placed after the first ) at the end 最后一个(“)应该放在第一个)之后

New code: 新代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM