Home  >  Q&A  >  body text

Please help, the database insertion operation failed.

The code is as follows:

$con = new mysqli($servername, $username, $password, $dbname);
if ($con->connect_error) {
die(" Database connection failed: " . $conn->connect_error);
}
echo "Database connection successful!";
for($i = 0 ; $i < 20 ; $i )
{
echo "Enter loop!<br/>";
$m = $i 1;
$sql = "insert into stuAnswer values('$m','$uid', '$tihao[$i]','$anser[$i]','$bzhun[$i]','$time_ks')";
$result = $con->query($sql) ;
if($result)
{
                  echo "Insertion successful!<br/>";
}
}



##Results of the:

qiuz.png

长天一色长天一色2187 days ago1744

reply all(8)I'll reply

  • 长天一色

    长天一色2018-09-29 11:00:56

    Thank you very much for your answers. I just found the crux of the problem. It is because the field names of the data table stuAnswer use Chinese names, which makes the data unable to be inserted. If you change it to English field names, you can insert it normally. Thanks again for everyone's guidance and enthusiasm.

    reply
    0
  • 学先森

    学先森2018-09-29 09:45:30

    Is it a database primary key conflict

    reply
    0
  • 长天一色

    No primary key set

    长天一色 · 2018-09-29 10:33:05
  • 神沐

    神沐2018-09-28 15:51:42

    $sql = "insert into stuAnswer values(".$m.",".$uid.",".$tihao[$i].",".$anser[$i].",".$bzhun[$i].",".$time_ks.")";

    Try replacing the sql statement with this one

    reply
    0
  • 长天一色

    Thank you, I tried it, but it still doesn't work

    长天一色 · 2018-09-28 18:52:31
  • jin

    jin2018-09-28 10:47:03

    You can print out the sql and execute it with phpmyadmin. Personally, I feel there is something wrong with the parameters in your values.

    reply
    0
  • 长天一色

    The sql statement can be executed under mysql. Moreover, after I added the echo statement to output the parameter data before $sql in the program just now, the variable data can be output. I really don’t understand why the insertion operation cannot be performed. Thank you very much for your enthusiasm!

    长天一色 · 2018-09-28 11:16:33
  • 长天一色

    长天一色2018-09-28 09:50:00

    It can be seen from the above execution results that the database connection is normal, but the insertion operation is not performed. I have read a lot of online information and cannot find the error in the code itself. I am a novice. Please give me some advice. Thank you!

    reply
    0
  • 长天一色

    Another point to add is that the order of fields in the data table is the same as the order of inserting values.

    长天一色 · 2018-09-28 09:54:12
  • Cancelreply