Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting Multiple Rows with PHP & MySQL
    primarykey
    data
    text
    <p>I'm building a website where people can place orders and this is the first time I've had to insert multiple rows at a time and I'm lost. I know that I need a FOR loop to perform this, but I'm lost as to how to construct the loop. I'm using PHP, MySQL (obviously) with jQuery. I'm using the jQuery to .append() a new select box into the form to allow the client to choose another item.</p> <p>This is how I usually construct my code to allow users to insert into the database. My question is how and where would I insert a loop that way multiples rows can be submitted all at once without having to insert them one by one. Anything would be helpful, thank you.</p> <pre><code>&lt;?php if (isset($_POST['submit'])) { if (!$_POST['col1'] | !$_POST['col2'] | !$_POST['col3']) { die ("error"); } if (!get_magic_quotes_gpc()) { $_POST['col1'] = addslashes ($_POST['col1']); $_POST['col2'] = addslashes ($_POST['col2']); $_POST['col3'] = addslashes ($_POST['col3']); } $insert = "insert into table (col1, col2, col3) values ('".$_POST['col1']."', '".$_POST['col2']."', '".$_POST['col3']."')"; mysql_query ($insert); } else { ?&gt; &lt;form action="&lt;?php echo $_SERVER['PHP_SELF'] ?&gt;" method="post"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" name="col1"&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="col2"&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="col3"&gt;&lt;/td&gt; //I'm using jQuery .append() to insert more text boxes with names (col1, col2, col3) here &lt;/tr&gt; &lt;/table&gt; &lt;input type="submit" name="submit" value="Submit"&gt; &lt;/form&gt; &lt;?php } ?&gt; </code></pre> <p>My confusion is where to put the loop... I know it should be a FOR loop, but I could never get one to work. Thanks again for any help.</p>
    singulars
    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