title: INSERT multiple records in MySQL with one PHP form
body: <p>INSERT multiple records in MySQL with one PHP form.  </p>
  
  <p>Simple Form</p>
  
  <pre><code>&lt;form action="process.php" method="post"&gt;
  &lt;p&gt;&lt;label&gt;Beamline ID&lt;/label&gt;
  &lt;input type="text" name="bline_id[][bline_id]" /&gt;
  &lt;label&gt;Flow&lt;/label&gt;
  &lt;input type="text" name="flow[][flow]" /&gt;
  &lt;/p&gt;
  &lt;p&gt;&lt;label&gt;Beamline ID&lt;/label&gt;
  &lt;input type="text" name="bline_id[][bline_id]" /&gt;
  &lt;label&gt;Flow&lt;/label&gt;
  &lt;input type="text" name="flow[][flow]" /&gt;
  &lt;/p&gt;
  &lt;p&gt;&lt;label&gt;Beamline ID&lt;/label&gt;
  &lt;input type="text" name="bline_id[][bline_id]" /&gt;
  &lt;label&gt;Flow&lt;/label&gt;
  &lt;input type="text" name="flow[][flow]" /&gt;
  &lt;/p&gt;
  &lt;p&gt;&lt;label&gt;Beamline ID&lt;/label&gt;
  &lt;input type="text" name="bline_id[][bline_id]" /&gt;
  &lt;label&gt;Flow&lt;/label&gt;
  &lt;input type="text" name="flow[][flow]" /&gt;
  &lt;/p&gt;
  &lt;p&gt;&lt;label&gt;Beamline ID&lt;/label&gt;
  &lt;input type="text" name="bline_id[][bline_id]" /&gt;
  &lt;label&gt;Flow&lt;/label&gt;
  &lt;input type="text" name="flow[][flow]" /&gt;
  &lt;/p&gt;
  &lt;input name="Submit" type="submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>//process.php</p>
  
  <pre><code>&lt;?php
  // connect to the database
  include('connect-db.php');
  
  
  $cnt = count($_POST['bline_id']);
  $cnt2 = count($_POST['flow']);
  
  if ($cnt &gt; 0 &amp;&amp; $cnt == $cnt2) {
      $insertArr = array();
      for ($i=0; $i&lt;$cnt; $i++) {
          $insertArr[] = "('" . mysql_real_escape_string($_POST['bline_id'][$i]) . "', '" . mysql_real_escape_string($_POST['flow'][$i]) . "')";
  }
  
   $query = "INSERT INTO bltest (bline_id, flow) VALUES " . implode(", ", $insertArr);
   mysql_query($query) or trigger_error("Insert failed: " . mysql_error());
  }
  
  echo("&lt;pre&gt;\n");
  print_r($_POST);
  echo("&lt;/pre&gt;\n");
  
  
  
  
  mysql_close($connection);
  ?&gt; 
  </code></pre>
  
  <p>Array results </p>
  
  <pre><code> Array
   (
   [bline_id] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [bline_id] =&gt; 1
              )
  
          [1] =&gt; Array
              (
                  [bline_id] =&gt; 2
              )
  
          [2] =&gt; Array
              (
                  [bline_id] =&gt; 3
              )
  
          [3] =&gt; Array
              (
                  [bline_id] =&gt; 4
              )
  
          [4] =&gt; Array
              (
                  [bline_id] =&gt; 5
              )
  
      )
  
  [flow] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [flow] =&gt; 11
              )
  
          [1] =&gt; Array
              (
                  [flow] =&gt; 22
              )
  
          [2] =&gt; Array
              (
                  [flow] =&gt; 33
              )
  
          [3] =&gt; Array
              (
                  [flow] =&gt; 44
              )
  
          [4] =&gt; Array
              (
                  [flow] =&gt; 55
              )
  
      )
  
  [Submit] =&gt; Submit Query
  )
  </code></pre>
  
  <p>the INSERT result is of course 5 rows but no data inserted for $bline_id or $flow. But looking at the array, that is the correct data.</p>