title: MySQLi prepared statement & foreach loop
body: <p>I'm struggling to revise the following (which works) to use a prepared statement:</p>
  
  <pre><code>echo "&lt;div class=\"debug\"&gt;
              &lt;h4&gt;values \$_POSTed from *LINE-ITEMS TABLE* in input.php:&lt;/h4&gt;
              &lt;table&gt;";
  
      foreach ($_POST['date'] as $i =&gt; $value) {
          $invNum     = $_POST['invNum'];
          $date           = $_POST['date'][$i];
          $hours      = $_POST['hours'][$i];
          $rate           = $_POST['rate'][$i];
          $dateTotal  = $_POST['dateTotal'][$i];
  
          echo "&lt;tr&gt;
                      &lt;td&gt;".$i."&lt;/td&gt;
                      &lt;td&gt;".$date."&lt;/td&gt;
                      &lt;td&gt;".$hours."&lt;/td&gt;
                      &lt;td&gt;".$rate."&lt;/td&gt;
                      &lt;td&gt;".$dateTotal."&lt;/td&gt;
                  &lt;/tr&gt;";
  
              $query = "INSERT INTO Invoice_Line_Items SET
                  INVOICE_NUMBER  = '$invNum',
                  DATE                = '$date',
                  HOURS               = '$hours',
                  RATE                = '$rate',
                  DATE_TOTAL      = '$dateTotal'
  
                  ON DUPLICATE KEY UPDATE
                  INVOICE_NUMBER  = VALUES(INVOICE_NUMBER),
                  DATE                = VALUES(DATE),
                  HOURS               = VALUES(HOURS),
                  RATE                = VALUES(RATE),
                  DATE_TOTAL      = VALUES(DATE_TOTAL)
                  ";
  
          } // END foreach
  echo "&lt;/table&gt;&lt;/div&gt;";
  </code></pre>
  
  <p>I've been trying to adapt the (working) prepared statement/query running above this in the same page, which inserts a single row into a different table. But this 2nd query (into a different db table) inserts data from multiple (dynamic # of) rows from a line-items table within the source form.</p>
  
  <p>I've been hacking at it for hours but I can't quite sort out how to implement a prepared statement with the line-items loop. I thought it would be along these lines, but this is not inserting.</p>
  
  <pre><code>echo "&lt;div class=\"debug\"&gt;
              &lt;h4&gt;values \$_POSTed from *LINE-ITEMS TABLE* in input.php:&lt;/h4&gt;
              &lt;table&gt;";
  
  // this is the line-items table in the form; don't I have to get these values before the query?
  foreach ($_POST['date'] as $i =&gt; $value) {
      $invNum     = $_POST['invNum'];
      $date           = $_POST['date'][$i];
      $hours      = $_POST['hours'][$i];
      $rate           = $_POST['rate'][$i];
      $dateTotal  = $_POST['dateTotal'][$i];
      // confirm vars/values
      echo "&lt;tr&gt;&lt;td&gt;".$i."&lt;/td&gt;&lt;td&gt;".$date."&lt;/td&gt;&lt;td&gt;".$hours."&lt;/td&gt;&lt;td&gt;".$rate."&lt;/td&gt;&lt;td&gt;".$dateTotal."&lt;/td&gt;&lt;/tr&gt;";
  
      $stmt = $mysqli-&gt;stmt_init();
      $query = "INSERT INTO Invoice_Line_Items
          INVOICE_NUMBER  = '$invNum',
          DATE                = '$date',
          HOURS               = '$hours',
          RATE                = '$rate',
          DATE_TOTAL      = '$dateTotal'
  
          ON DUPLICATE KEY UPDATE
          INVOICE_NUMBER  = VALUES(INVOICE_NUMBER),
          DATE                = VALUES(DATE),
          HOURS               = VALUES(HOURS),
          RATE                = VALUES(RATE),
          DATE_TOTAL      = VALUES(DATE_TOTAL)
          ";
  
          if ($stmt-&gt;prepare($query)) {
              $stmt -&gt; bind_param("ssddd", $invNum, $date, $hours, $rate, $dateTotal);
              $stmt -&gt; execute();
              $stmt-&gt;close();
          } // if $stmt
  } // END foreach
  
  echo "&lt;/table&gt;&lt;/div&gt;";
  </code></pre>
  
  <p>Can someone please shed some light? Much appreciated.</p>
  
  <p>svs</p>