2017-07-24 4 views
0

Mein Code ist wie folgt: Ich versuche, in meine Datenbank mit mehreren Einfügungen gleichzeitig mit einer mysqli_query einfügen, wie gehe ich darüber?Ich habe versucht, eine mysqli_query mit mehreren Einsätzen auszuführen, aber es funktioniert nicht

$sql = "INSERT INTO questions (exam_type, questions, option_a, option_b, option_c, answer) 
     VALUES ('$q_type', '$q1', '$q1a', '$q1b', '$q1c', '$q1answer'); 
     INSERT INTO questions (exam_type, questions, option_a, option_b, option_c, answer) 
     VALUES ('$q_type', '$q2', '$q2a', '$q2b', '$q2c', '$q2answer'); 
     INSERT INTO questions (exam_type, questions, option_a, option_b, option_c, answer) 
     VALUES ('$q_type', '$q3', '$q3a', '$q3b', '$q3c', '$q3answer'); 
     INSERT INTO questions (exam_type, questions, option_a, option_b, option_c, answer) 
     VALUES ('$q_type', '$q4', '$q4a', '$q4b', '$q4c', '$q4answer'); 
     INSERT INTO questions (exam_type, questions, option_a, option_b, option_c, answer) 
     VALUES ('$q_type', '$q5', '$q5a', '$q5b', '$q5c', '$q5answer') 

"; 

echo $sql ."<br>"; 
require "connect.php"; 

if($results= mysqli_query($con, $sql)) 
{ 

    echo "saved successfully"; 

} 
+6

Hallo und Willkommen zu Stackoverflow. Nun, die Antwort ist ganz einfach, aber ich werde sie jetzt nicht beantworten. Zunächst bitte ich Sie, Ihre Frage zu bearbeiten. Formatiere es und bringe weitere Informationen mit, damit du lernst, wie man richtig fragt. Nachdem Sie das getan haben, beantworte ich Ihre Frage. Die Lösung ist ziemlich einfach. – Twinfriends

+1

Suche PHP-String-Interpolation. – aristotll

+0

"aber es funktioniert nicht" nichts einfügen? erst einlegen? Welcher Fehler kommt zurück? Spalten der Tabelle? – Cuchu

Antwort

-1

Der SQL-Teil so sein shoud.

$sql = "INSERT INTO questions (exam_type, questions, option_a, option_b, option_c, answer) 
     VALUES ('$q_type', '$q1', '$q1a', '$q1b', '$q1c', '$q1answer'), 
       ('$q_type', '$q2', '$q2a', '$q2b', '$q2c', '$q2answer'), 
       ('$q_type', '$q3', '$q3a', '$q3b', '$q3c', '$q3answer'), 
       ('$q_type', '$q4', '$q4a', '$q4b', '$q4c', '$q4answer'), 
       ('$q_type', '$q5', '$q5a', '$q5b', '$q5c', '$q5answer')"; 

Der PHP-Teil ist in Ordnung.

0

Versuchen Sie einfach mit dieser Abfrage:

INSERT INTO questions (exam_type, questions, option_a, option_b, 
    option_c, answer) VALUES ('$q_type', '$q1', '$q1a', '$q1b', '$q1c', 
    '$q1answer'), ('$q_type', '$q2', '$q2a', '$q2b', '$q2c', 
    '$q2answer'), ('$q_type', '$q3', '$q3a', '$q3b', '$q3c', 
    '$q3answer'),('$q_type', '$q4', '$q4a', '$q4b', '$q4c', '$q4answer' 
    ),('$q_type', '$q5', '$q5a', '$q5b', '$q5c', '$q5answer') 
Verwandte Themen