简体   繁体   中英

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. 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:

$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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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