PHP MySQL Insert multiple records - do I need a loop?

I am trying to insert multiple records into a MySQL database. The problem im having is that when i hit submit, only the values from the last row are being entered into the db.  The variable is QTY, for quantity.

Here is an example of a row from the HTML.  I have about 10 of these, that I want to enter different quantities into the DB when I hit submit:

          <td><div align="center" class="main-12px-verdana">Da'NOLLIE</div></td>
          <td><div align="center" class="main-12px-verdana">160cm</div></td>
          <td><div align="center" class="main-12px-verdana">4501<input id="SKU" name="SKU" type="hidden" value="4501">

I know I need to loop through the values but im not quite sure how to do it.

I think that I need a loop to do this.  I'm using a helper program called Data Assist ( to insert the records.  Here is the code I am trying to figure out how to add a loop to:

Any guidance on this would be much appreciated.
<?php require_once('../Connections/icelanticordering.php'); ?>
<?php require_once("../WA_DataAssist/WA_AppBuilder_PHP.php"); ?>
// WA Application Builder Insert
if (isset($_POST["Insert"])) // Trigger
  $WA_connection = $icelanticordering;
  $WA_table = "Orders";
  $WA_sessionName = "WADA_Insert_Orders";
  $WA_redirectURL = "3uswholesale200809.php";
  $WA_keepQueryString = false;
  $WA_indexField = "OrderID";
  $WA_fieldNamesStr = "CustomerID|SKU|QTY";
  $WA_fieldValuesStr = "".((isset($_POST["CustomerID"]))?$_POST["CustomerID"]:"")  ."" . "|" . "".((isset($_POST["SKU"]))?$_POST["SKU"]:"")  ."" . "|" . "".((isset($_POST["QTY"]))?$_POST["QTY"]:"")  ."";
  $WA_columnTypesStr = "',none,''|',none,''|',none,''";
  $WA_fieldNames = explode("|", $WA_fieldNamesStr);
  $WA_fieldValues = explode("|", $WA_fieldValuesStr);
  $WA_columns = explode("|", $WA_columnTypesStr);
  $WA_connectionDB = $database_icelanticordering;
  mysql_select_db($WA_connectionDB, $WA_connection);
  if (!session_id()) session_start();
  $insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
  $WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
  $MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
  $_SESSION[$WA_sessionName] = mysql_insert_id();
  if ($WA_redirectURL != "")  {
    if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
      $WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
    header("Location: ".$WA_redirectURL);

Open in new window

Who is Participating?
dr_dedoConnect With a Mentor Commented:
since all your data are stored in one table, then you do need a loop to create a single sql. i mean, SKU and OTY are arrays, so u need to loop through them to extract the different values of each, and construct a single sql statment that looks like this

insert into table_name ('field1','field2','field3') values

constructing the coma separated part needs a loop, but a single mysql query
pankisAuthor Commented:
Sorry, here is a correct example of a row from my table.  I am trying to enter multiple Quantities (variable shown as QTY[] ) into the database from this form.

Thanks for your help.

          <td><div align="center" class="main-12px-verdana">SHAMAN</div></td>
          <td><div align="center" class="main-12px-verdana">161cm</div></td>
          <td><div align="center" class="main-12px-verdana">4401<input id="SKU" name="SKU" type="hidden" value="4401">
          <td><div align="center"><input id="QTY" name="QTY[]" type="text" size="5" maxlength="5" value=""></div></td>
          <td><div align="center" class="main-12px-verdana">$377.00</div></td>
          <td><div align="center" class="main-12px-verdana">$649.00</div></td>
for a start, change name="SKU" to name="SKU[]" and see if SKU is inserted into db
pankisAuthor Commented:
Thanks, that was an oversight on my part, I've made that change now.

In the DB, I now get "Array" and "Array" for SKU and QTY fields.

I *think* that I want to have individual records created for each order, as each order will have an appropriate CustomerID attached to it.  This is why I *think* I need to have a loop, to loop through each SKU/QTY combination, to create individual orders.

So, I still think I need a loop of some sort.  Any help on that side?
pankisAuthor Commented:
Thanks for your help.
All Courses

From novice to tech pro — start learning today.