title: How insert multiple rows in mysql table with php array?
body: <p>I need to insert entries to mysql table from the form below.
  1-form contains many rows.
  2-entry will not be always consecutive in the rows (meaning row 1 can be empty and next row not)
  3-all rows containing entries should be saved in the db table.</p>
  
  <p>i want to INSERT INTO oz2ts_custompc_details (part_id, quantity, price)</p>
  
  <p>Here is my entry form (custompc_form2.php)</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;form action="../subs/custompcorder2.php/" method="post" id="form"&gt;
  
              &lt;p&gt;&lt;input id="name" name="part_id[]"/&gt; 
                 &lt;input type="text"  id="quantity" name="quantity[]"/&gt;  
                 &lt;input id="name-data" type="text" name="price[]"/&gt;&lt;/p&gt;
  
              &lt;p&gt;&lt;input id="name" name="part_id[]"/&gt; 
                 &lt;input type="text" id="quantity" name="quantity[]"/&gt;  
                 &lt;input id="name-data" type="text" name="price[]"/&gt;&lt;/p&gt;
  
              &lt;p&gt;&lt;input id="name" name="part_id[]"/&gt; 
                 &lt;input type="text" id="quantity" name="quantity[]"/&gt; 
                 &lt;input id="name-data" type="text" name="price[]"/&gt;&lt;/p&gt;
  
              &lt;p&gt;&lt;input id="name" name="part_id[]"/&gt; 
                 &lt;input type="text" id="quantity" name="quantity[]"/&gt; 
                 &lt;input id="name-data" type="text" name="price[]"/&gt;&lt;/p&gt;   
  
  
      &lt;input id="submit" type="submit" value="Submit Order" name="submission"/&gt;
  
  
  &lt;/form&gt;
  &lt;/body&gt; 
  &lt;/html&gt;
  </code></pre>
  
  <p>here is What I came up with but still not working.
  here is the summary of how it is working: ||Rows 1 to 4 has data > all 4 are saved || row 1 is empty and rows 2 to 3 contains data > only rows 2 and 3 are saved not row 4|| Row 2 only has data all other are empty > Data not saved || Rows 2 and 3 has data > Row 2 only is saved</p>
  
  <pre><code> &lt;?php
  include '../db/connect.php';
  
  
  foreach (array('part_id', 'quantity', 'price') as $pos) {
  foreach ($_POST[$pos] as $id =&gt; $row) {
      $_POST[$pos][$id] = mysqli_real_escape_string($con, $row);
  }
  }
  
  $ids = $_POST['part_id'];
  $quantities = $_POST['quantity'];
  $prices =  $_POST['price'];
  
  $items = array();
  
  $size = count($ids);
  
  for($i = 0 ; $i &lt; $size ; $i++){
  // Check for part id
  if (empty($ids[$i]) || empty($quantities[$i]) || empty($prices[$i])) {
      continue;
  }
  $items[]=array(
      "part_id"     =&gt; $ids[$i], 
      "quantity"    =&gt; $quantities[$i],
      "price"       =&gt; $prices[$i]
  );
  }
  
  if (!empty($items)) {
  $values = array();
  foreach($items as $item){
      $values[] = "('{$item['part_id']}', '{$item['quantity']}', '{$item['price']}')";
  }
  
  $values = implode(", ", $values);
  
  $sql = "INSERT INTO oz2ts_custompc_details (part_id, quantity, price) VALUES     {$values}    ;
  " ;
  $result = mysqli_query($con, $sql );
  if ($result) {
      echo 'Successful inserts: ' . mysqli_affected_rows($con);
  } else {
      echo 'query failed: ' . mysqli_error($con);  
  
  }
  }
  
  ?&gt; 
  </code></pre>
  
  <p>The first is a simplified entry form. The reel entry form looks like this:</p>
  
  <pre><code>    &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;
  
      &lt;form action="../subs/custompcorder2.php/" method="post" id="form"&gt;
  
            &lt;div id="orderwrap"&gt;
           &lt;div id="orderheather"&gt;
          &lt;select id="platform" name="platform"&gt;
          &lt;option selected="selected" disabled="disabled"&gt;Select the 
                      platform&lt;/option&gt;
          &lt;option value="Intel"&gt;Intel&lt;/option&gt;
          &lt;option value="AMD"&gt;AMD&lt;/option&gt;
         &lt;/select&gt; 
      &lt;/div&gt; 
  
         &lt;div id="orderbody"&gt;
  
           &lt;p&gt;&lt;select id="part_id" name="part_id[]"&gt;
                          &lt;option selected="selected" disabled="disabled"&gt;Choose part1 &lt;/option&gt;
                   &lt;?php  query() ?&gt; 
                   &lt; /select&gt;
                           &lt;input type="text" id="quantity" name="quantity[]"/&gt; 
                           &lt;input id="name-data" type="text" name="price[]"/&gt;&lt;/p&gt;
  
           &lt;p&gt;&lt;select id="part_id" name="part_id[]"&gt;
                          &lt;option selected="selected" disabled="disabled"&gt;Choose part2 &lt;/option&gt;
                   &lt;?php  query2() ?&gt; 
                   &lt; /select&gt;
                           &lt;input type="text" id="quantity" name="quantity[]"/&gt; 
                           &lt;input id="name-data" type="text" name="price[]"/&gt;&lt;/p&gt; 
  
           &lt;p&gt;&lt;select id="part_id" name="part_id[]"&gt;
                          &lt;option selected="selected" disabled="disabled"&gt;Choose part3 &lt;/option&gt;
                   &lt;?php  query3() ?&gt; 
                   &lt; /select&gt;
                           &lt;input type="text" id="quantity" name="quantity[]"/&gt; 
                           &lt;input id="name-data" type="text" name="price[]"/&gt;&lt;/p&gt; 
  
           &lt;p&gt;&lt;select id="part_id" name="part_id[]"&gt;
                          &lt;option selected="selected" disabled="disabled"&gt;Choose part4 &lt;/option&gt;
                   &lt;?php  query4() ?&gt; 
                   &lt; /select&gt;
                           &lt;input type="text" id="quantity" name="quantity[]"/&gt; 
                           &lt;input id="name-data" type="text" name="price[]"/&gt;&lt;/p&gt;  
  
  
  
          &lt;input id="submit" type="submit" value="Submit Order"name="submission"/&gt;
  
         &lt;/div&gt;
        &lt;/div&gt;    
          &lt;/form&gt;
  
  
        &lt;/body&gt; 
  
       &lt;/html&gt;
  </code></pre>
  
  <p>Here is the php page containing function query(),query1(),..</p>
  
  <pre><code>&lt;?php
  include '../db/connect.php';
  
  
  function query(){
  global $con; 
  $myData=mysqli_query($con,"SELECT * FROM oz2ts_mijoshop_product");
  while($record=mysqli_fetch_array($myData)){
      echo'&lt;option value="'.$record['product_id'].'"&gt;'.$record['model'].'&lt;/option&gt;';
      }
  }
  
  function query2(){
  global $con; 
  $myData=mysqli_query($con,"SELECT * FROM oz2ts_mijoshop_product");
  while($record=mysqli_fetch_array($myData)){
      echo'&lt;option value="'.$record['product_id'].'"&gt;'.$record['model'].'&lt;/option&gt;';
      }
  }
  
  
  function query3(){
  global $con; 
  $myData=mysqli_query($con,"SELECT * FROM oz2ts_mijoshop_product");
  while($record=mysqli_fetch_array($myData)){
      echo'&lt;option value="'.$record['product_id'].'"&gt;'.$record['model'].'&lt;/option&gt;';
      }
  }
  
  
  function query4(){
  global $con; 
  $myData=mysqli_query($con,"SELECT * FROM oz2ts_mijoshop_product");
  while($record=mysqli_fetch_array($myData)){
      echo'&lt;option value="'.$record['product_id'].'"&gt;'.$record['model'].'&lt;/option&gt;';
      }
  }
  
  function close(){
      mysqli_close($con);
      }
  
  
  ?&gt;
  </code></pre>