Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting data into two or more tables:
    text
    copied!<p>I have the following tables:</p> <pre><code>thread: id, title, content, created thread_tags: tag_id, thread_id tag: id, name author_threads: thread_id, author_id </code></pre> <p>In order to create a thread all of these fields must be filled in (The ids are obviously automatically incremented). I know that I can use SQL TRANSACTIONS to make sure all of them are filled in or none, however how do I go about filling in tag_id, thread_id, author_id and thread_id (in author_threads) from the last sql statement? Without the transaction I would use mysqli_last_insert_id.</p> <p>Also should I use <code>mysqli::multi_query</code>? Basically how do I go about making sure all these fields are filled in?</p> <p>Oh, and I'm using PHP with MYSQL.</p> <p><strong>EDIT:</strong></p> <p>Would this work?</p> <pre><code> $sql_thread = "START TRANSACTION; INSERT INTO thread (title, content) VALUES ('some title', 'some content')"; # this is normally a loop, as there are more than one tags: $sql_tags = "INSERT INTO tag (name) VALUES ('onetag')"; # normally I would check the return value mysqli_query($link, $sql_thread); # get the thread id: $thread_id = mysqli_insert_id($link); mysqli_query($link, $sql_tags); # get the tag id: $tag_id = mysqli_insert_id($link); # insert into thread_tags: mysqli_query($link, "INSERT INTO thread_tags (thread_id, tag_id) VALUES ($thread_id, $tag_id)"); # insert into author_threads, I already know author_id: mysqli_query($link, "INSERT INTO author_threads (author_id, thread_id) VALUES ($author_id, $thread_id) COMMIT;"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload