简体   繁体   中英

Insert into multiple tables using auto-increment key

What I am thinking about doing might not be possible, I'm not sure. Here's my situation:

I put together a project for class that allows a teacher to create quizzes for students to take. In php where the test questions/answers are entered into the database I am using mysqli prepared statements. Since I am using the question/answer ids to link them I set it up like so:

$q_stmt = $db -> prepare('query with no correct_answer_id')
$a_stmt = $db -> prepare('query with ? = $q_stmt -> insert_id')
$a_stmt -> bind_params($q_id)
$q_update = $db -> prepare('query with ? = $a_stmt -> insert_id for correct answer')
$q_update -> bind_params($correct_answer_id)

and then the script loops like:

foreach ($question)
  $q_stmt -> execute()
  $q_id = $db -> insert_id
  foreach ($answers)
     $a_stmt -> execute()
     id (correct_answer) $correct_answer_id = $db -> insert_id
$q_update -> execute()

Now, this works on my laptop (Mac OS X.6 with latest (as of january) PHP and MySQL) but when I uploaded another part of my project that also used nested prepared statements to my webserver I got an error message saying that it didn't appreciate my nesting. It says:

Warning: mysqli::prepare() [mysqli.prepare]: All data must be fetched before a new statement prepare takes place in root/path/file-name .php on line 124

So, from that I assume that nesting prepared statements must be a bad thing? That is quite unfortunate since I really liked how I put that together.

As an alternative, I could loop through all the questions gathering ids and then do the loop again on the questions and answers also gathering ids and then loop yet one more time to update the questions but that seems like a lot of extra work for the server and I like to try to be efficient.

That said, I am looking for one of two things.
1 - by some miracle there is a way to run my query in one step
a - I would enjoy a solution that allowed for the question/correct answer pair to insert at one time and then loop later for the variable number of incorrect answers also associated to the question
2 - a way to change my web server (I have full access to cpannel) such that it no longer hates on my nested prepared statements

Thanks in advance

The core problem is that one of the prepared statements has a result set for you, but you aren't retrieving it before trying the next statement. Some versions of the MySQL libraries have problems with this, while others don't.

Take a look at mysqli_stmt::store_result . You can call it immediately after calling execute to transfer the result set out of MySQL. Even if you won't be working with the result set immediately (or at all?), this action should let you continue to execute your other prepared statements.

(In PDO land, this can be controlled by toggling buffered queries .)

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