Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/MySQL syntax to insert into multiple tables (many-to-many structure)
    primarykey
    data
    text
    <p>I am trying to insert data from one form into multiple MySQL tables (using MYSQL syntax, although I will be migrating the application MYSQLi at some point - this probably isn't relevant right now, but thought I'd say it to stop the "<em>you should be doing this in MYSQLi</em>" comments that inevitably follow - I know I should, and I will be... soon... but for a number of reasons out of my control, I can't yet, and we still need this working).</p> <p>Essentially, I am inserting into a "many-to-many" table structure (i.e. instances that can be part of many categories, and categories that can include many instances). The main table <code>Instances</code>, has all of the main pieces of data (in this case, places on a map - for example 'Acme Cafe', 'Lakeside Picnic Area', etc); another table <code>Categories</code> includes all of the possible categories for all of the places in the <code>instances</code> table - for example, 'Cafe', 'Pub', 'View Point', etc; and a joining table <code>Instance_Categories</code>, connects the places in the <code>Instances</code> table to the categories in the <code>Categories</code> table.</p> <p>The PHP form has the following fields:</p> <ul> <li><code>Reference</code> (a 9 digit randomly generated identifier used to identify the instance with it's many categories in the <code>Instance_Categories</code> table)</li> <li><code>Name</code> (text box)</li> <li><code>Address</code> (text box)</li> <li><code>Phone</code> (text box)</li> <li><code>Tick-box</code> fields for each of the categories (8 of them).</li> </ul> <p>The <code>Instances</code> table has the following columns</p> <ul> <li><code>ID</code> (unique for the table)</li> <li><code>Reference</code> (see form input above)</li> <li><code>Name</code></li> <li><code>Address</code></li> <li><code>Phone</code></li> </ul> <p>The <code>Categories</code> table has the following columns</p> <ul> <li><code>ID</code> (unique to the table)</li> <li><code>Category</code> (varchar)</li> <li><code>Category_Ref</code> (a number to identify the reference in the connecting <code>Instances_Categories</code> table)</li> </ul> <p>The <code>Instances_Categories</code> table has the following columns:</p> <ul> <li><code>ID</code> (unique to the table)</li> <li><code>Instance_Ref</code> (from the form input above)</li> <li><code>Category_Ref</code> (relating to the <code>Categories</code> table above)</li> </ul> <p>How do I formulate the <code>INSERT</code> statement in order to insert the data into my database? Can I just formulate an insert command for each table and run them one after the other from the same "Submit" button? And how would I get the insert command to dynamically insert more rows into the <code>Instances_Categories</code> table depending on how many category tick-boxes have been selected? I'm tearing my hair out!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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