Note that there are some explanatory texts on larger screens.

plurals
  1. POphp mysqli insert multiple rows to 1 table and 1 row to another table
    primarykey
    data
    text
    <p>I've been working on this with help from this site. I can now insert into <code>players</code> table multiple rows (based on <code>$add-rows</code> value). I need to, also, insert into the <code>events</code> table 1 row. when I submit the form, it inserts into <code>players</code> fine but not into <code>events</code></p> <p>This is my form with all values needed for 2 queries</p> <pre><code>&lt;form id="form-add" name="form-add" method="POST" enctype="multipart/form-data" action="page.php"&gt; &lt;input name="add-name" id="add-name" type="text" value="Event"&gt; &lt;input name="add-start" id="add-start" type="text" value=""&gt; &lt;input name="add-end" id="add-end" type="text" value=""&gt; &lt;input name="add-loc" id="add-loc" type="text" value=""&gt; &lt;input name="add-rows" id="add-rows" type="text" value=""&gt; &lt;input name="add-submit" id="add-submit" type="submit" value="Add" /&gt; &lt;input type="hidden" name="submitted" value="TRUE" /&gt; &lt;/form&gt; </code></pre> <p>These are the values I'm posting from the form</p> <pre><code>&lt;?php $add_name = $_POST['add-name']; $add_start = $_POST['add-start']; $add_end = $_POST['add-end']; $add_loc = $_POST['add-loc']; $add_rows = $_POST['add-rows']; $add_url = date('Y-m-d',strtotime($add_start)).'-'.str_replace('-',' ',($add_name)); if(isset($_POST['submitted'])) { //check if form submitted //connection $mysqli = new mysqli('host', 'user', 'pass', 'db_name'); //this is the first query - to insert multiple rows in to players table (from same form) $query = "INSERT INTO players (position, event, start, end, name, one, two, three, four, five, six) VALUES ('', '$add_url', '$add_start', '$add_end', '', 'Yes', 'No', 'No', 'No', 'No', 'No');" . str_repeat(", ('', '$add_url', '$add_start', '$add_end', '', 'Yes', 'No', 'No', 'No', 'No', 'No')", $add_rows - 1); //this is the 2nd query - to insert to events table (from same form) $query .= "INSERT INTO events (ur, name, start, end, loc) VALUES ('$add_url', '$add_name' '$add_start', '$add_end', '$add_loc');"; // execute query - $result is false if the first query failed $result = mysqli_multi_query($mysqli, $query); if ($result) { do { // grab the result of the next query if (($result = mysqli_store_result($mysqli)) === false &amp;&amp; mysqli_error($mysqli) != '') { echo "Query failed: " . mysqli_error($mysqli); } } while (mysqli_more_results($mysqli) &amp;&amp; mysqli_next_result($mysqli)); // while there are more results } else { echo "First query failed..." . mysqli_error($mysqli); } }//end of form submit if ?&gt; </code></pre>
    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