title: Loop through rows and INSERT multiple rows into MySQL
body: <p>In a nutshell, I'm looping and trying to INSERT all the rows but it only inserts the last row for as many times that it loops.  So, for two loops, it will loop through twice, and insert the last row twice (not inserting the first row at all).</p>
  
  <p>I'm using this as the row / items counter:</p>
  
  <pre><code>$countRecords;
  </code></pre>
  
  <p>This is a basic counter that tells me how many rows are included in the previous invoice.  This works.  In my example, it displays "2" as two items (rows) are in the previous invoice.</p>
  
  <p>I'm using the first part of the MySQL outside of the loop:</p>
  
  <pre><code>// the MySQL statement out of the loop
  $stmt="INSERT INTO o70vm_invoices_items
      (`id`, `invoice_id`, `name`, `desc`, `value`, `amount`, `discount`, `ordering` ) VALUES ";
  </code></pre>
  
  <p>I'm then running the loop:</p>
  
  <pre><code>// loop through the rows
  for($rowCounter = 0; $rowCounter &lt; $countRecords; $rowCounter++) 
      {
  
  
      // assign variables based on each loop iteration
      $invoiceID = htmlentities($_POST['invoice_id'], ENT_QUOTES);
      $program = htmlentities($_POST['name'], ENT_QUOTES);
      $forWeek = htmlentities($_POST['desc'], ENT_QUOTES);
      $value = htmlentities($_POST['value'], ENT_QUOTES);
      $qty = htmlentities($_POST['amount'], ENT_QUOTES);
  
  
      // put the VALUES array string for each row as such
      $ValuesAddToQuery[] ="(NULL, '$invoiceID', '$program', '$forWeek', '$value', '$qty','0.00','1')";
  
      }
  </code></pre>
  
  <p>After the loop, I implode the $ValuesAddToQuery array to insert as such:</p>
  
  <pre><code>// add all the values into the statement
  $stmt .= implode(',',$ValuesAddToQuery);
  
  echo $stmt;
  
  // execute statement
  //mysql_query($stmt) or exit(mysql_error());
  </code></pre>
  
  <p>As I said, this loop only inserts the last row the same number of times as it loops through the code. </p>
  
  <p>My <b>var_dump</b> shows that I have the last row added twice in the array also.</p>
  
  <p>So much appreciate any help on this.</p>
  
  <p>For an uncut version of my loop, here it is:</p>
  
  <pre><code>                $stmt="INSERT INTO o70vm_invoices_items 
                  (`id`, `invoice_id`, `name`, `desc`, `value`, `amount`,`discount`,`ordering` ) VALUES ";
  
                  // loop through the rows
                  for($rowCounter = 0; $rowCounter &lt; $countRecords; $rowCounter++) 
                      {
  
  
                      // assign variables based on each loop iteration
                      $invoiceID = htmlentities($_POST['invoice_id'], ENT_QUOTES);
                      $program = htmlentities($_POST['name'], ENT_QUOTES);
                      $forWeek = htmlentities($_POST['desc'], ENT_QUOTES);
                      $value = htmlentities($_POST['value'], ENT_QUOTES);
                      $qty = htmlentities($_POST['amount'], ENT_QUOTES);
  
  
                      // put the VALUES array string for each row as such
                      $ValuesAddToQuery[] ="(NULL, '$invoiceID', '$program', '$forWeek', '$value', '$qty','0.00','1')";
  
  
                      }
  
                      // add all the values into the statement
                      $stmt .= implode(',',$ValuesAddToQuery);
  
                      echo $stmt."&lt;br /&gt;&lt;br /&gt;";
  
                      var_dump($ValuesAddToQuery);
  
                      // execute statement
                      //mysql_query($stmt) or exit(mysql_error());
  </code></pre>
  
  <p>For a little more detail, I'm getting the data from the previous invoice here:</p>
  
  <pre><code>    &lt;td&gt;
      &lt;input type="number" title="'.$row-&gt;invoice_id_on_items.'" name="invoice_id" size="2" id="invoice_id" value="' . $row-&gt;invoice_id_on_items. '" /&gt;
  &lt;/td&gt;
  
  &lt;td&gt;(&lt;a href="getIndItems.php?id=' .$row-&gt;item_id. '" target="_blank"&gt;EDIT&lt;/a&gt;)&lt;br&gt;'.$row-&gt;item_id.'&lt;/td&gt;';
  
  $_SESSION['itemIDSelected']=$row-&gt;item_id;
  
  echo'
  
  &lt;input type="hidden" title="'.$row-&gt;item_id.'" name="id" size="13" id="id" value="'.$row-&gt;item_id. '" /&gt;
  
  &lt;td&gt;
      &lt;input type="text" title="'.$row-&gt;forweek.'" name="desc" size="15" id="desc" value="' . $row-&gt;forweek. '" /&gt;
  &lt;/td&gt;
  &lt;td&gt;
      &lt;input type="text" title="'.$row-&gt;program.'" name="name" size="50" id="name" value="' . $row-&gt;program. '" /&gt;
  &lt;/td&gt;
  &lt;td&gt;
      &lt;input type="number" title="'.$row-&gt;fee.'" name="value" size="3" id="value" value="' . $row-&gt;fee. '" /&gt;
  &lt;/td&gt;
  &lt;td&gt;
      &lt;input type="number" title="'.$row-&gt;qty.'" name="amount" size="3" id="amount" value="' . $row-&gt;qty. '" /&gt;
  &lt;/td&gt;
  ';
  $Fee = floatval($row-&gt;fee);
  $Qty = floatval($row-&gt;qty);
  $ItemFee=$Fee*$Qty;
  echo '
  &lt;td&gt;
      &lt;input type="text" title="'.$ItemFee.'" name="total_fee" size="3" id="total_fee" value="' . $ItemFee. '" /&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p>Then, I'm using the already illustrated above loop code to get the data.  I am able to retrieve the past invoice data without issue but wanted to include it in my question so you would have all the necessary information to provide guidance.</p>