0
 $sql = "INSERT INTO answer (trueOrFalse, answer)
    VALUES(?, ?);
   SET @idanswer01 = LAST_INSERT_ID();
  INSERT INTO answer (trueOrFalse, answer)
    VALUES(?, ?);
   SET @idanswer02 = LAST_INSERT_ID();
  INSERT INTO answer (trueOrFalse, answer)
    VALUES(?, ?);
   SET @idanswer03 = LAST_INSERT_ID();
  INSERT INTO questions (topic, question, answer1Id, answer2Id, answer3Id)
    VALUES(?, ?, @idanswer01, @idanswer02, @idanswer03);";

$stmt = mysqli_stmt_init($conn)

mysqli_stmt_prepare($stmt, $sql)

The mysqli_stmt_prepare is failing, I think it's because my sql query includes many statements. Is there a way to execute it or to rewrite the sql query to only be one statement?

[Picture of my two Tables answer and questions][1] [1]: https://i.stack.imgur.com/7ZRpx.png

Thomas
  • 13
  • 2
  • Just in case you need more information about splitting it into separate queries - https://stackoverflow.com/questions/19738283/mysqli-last-insert-id allows you to get the last insert id, but you can also prepare the answer query once and run it 3 times. – Nigel Ren May 21 '21 at 07:33
  • Can I also suggest that you change the database design slightly. Insert the question first and then have the question ID as a foreign key on the answer. This way you can have as many answers to a question you need, currently you have a fixed 3 answers. – Nigel Ren May 21 '21 at 07:35
  • Thank you for your answer, but what exactly do I need to do? Splitting the query into 4 querys and 4 prepared statements and executing them one by one? – Thomas May 21 '21 at 07:40
  • Prepare once, execute multiple times (bind and execute in a loop). – El_Vanja May 21 '21 at 07:43
  • The duplicate (plus the question I added in the comment) should be enough to get you going. But I would suggest you look at your database design as well. – Nigel Ren May 21 '21 at 07:44

0 Answers0