title: Multiple insert into Mysql from an html form using php
body: <p>Thanks so very much for all your help, Mysql DB works , now I am encountering a little problem, hope I'll fix it quickly and a litle help of course, so here My Html form :</p>
  
  <pre><code>&lt;div&gt;Date: 
      &lt;input onclick="ds_sh(this);" name="trans_date" readonly="readonly" style="cursor: text" /&gt;&lt;br/&gt;&lt;br/&gt;
      Product:
      &lt;select name="product_id []"&gt;
          &lt;option value="1"&gt;Item1&lt;/option&gt;
          &lt;option value="2"&gt;Item2&lt;/option&gt;
          &lt;option value="3"&gt;Item3&lt;/option&gt;
          &lt;option value="4"&gt;Item4&lt;/option&gt;
          &lt;option value="5"&gt;Item5&lt;/option&gt;
      &lt;/select&gt;
      Quantity:
      &lt;input type="text" name="stock_plus []" /&gt;&lt;br/&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This div is repeated 10 times or more, and I use that form to let user add inventory count of selected product</p>
  
  <p>Now when I tried to used PHP to insert rows on my table :</p>
  
  <p>1-Method 1 :</p>
  
  <p>PHP Code:</p>
  
  <pre><code>$product = $_POST['product_id']; $stock_plus = $_POST['stock_plus'];
  $Date = $_POST['trans_date']; $limit = count($stock_plus);
  for($i=0;$i&lt;$limit;$i++) {
      $product[$i] = mysql_real_escape_string($product[$i]);
      $stock_plus[$i] = mysql_real_escape_string($stock_plus[$i]);
  }
  
  $query = "INSERT INTO table (trans_date, product_id, stock_plus)
  VALUES ('".$Date."','".$product[$i]."','".$stock_plus[$i]."')";
  if(mysql_query($query))
      echo "$i successfully inserted.&lt;br/&gt;";
  else
      echo "$i encountered an error.&lt;br/&gt;";
  </code></pre>
  
  <p>I got : Notice: Undefined offset: 2...
  and not all the rows are inserted.</p>
  
  <p>Method 2 :</p>
  
  <p>PHP Code:</p>
  
  <pre><code>$trans_date=$_POST['trans_date']; $sql = 'INSERT INTO table
  (trans_date, product_id, stock_plus) VALUES ';
  
  for($i = 0;$i &lt; count($_POST['product_id']);$i++) {
      $sql .= "('$trans_date','".$_POST['product_id'][$i]."','".$_POST['stock_plus']i]."')";
      if($i != count($_POST['product_id']) - 1)
      {
          $sql .= ',';  
      } 
  }
  if (!mysql_query($sql))   {   die('Error: ' . mysql_error());   }
  </code></pre>
  
  <p>Here no errors but not all the rows are inserted.
  Can you help me please to see clearier what I missed,regards</p>
  
  <p>Additional :</p>
  
  <p>thanks Travesty3, I look too into myhtml for errors, My HTML body looks exactly like:</p>
  
  <pre><code>&lt;body&gt;
  &lt;form action="../inserts/stock_insert.php" method="post"&gt;
  &lt;div&gt;
  &lt;!-- JS Datepicker --&gt;
  Date: &lt;input onclick="ds_sh(this);" name="trans_date" readonly="readonly" style="cursor: text" /&gt;
  &lt;br/&gt;
  &lt;!-- User should select product --&gt;
  Product:&lt;select name="product_id []"&gt;
  &lt;option value="1"&gt;Item1&lt;/option&gt;
  &lt;option value="2"&gt;Item2&lt;/option&gt;
  &lt;option value="3"&gt;Item3&lt;/option&gt;
  &lt;option value="4"&gt;Item4&lt;/option&gt;
  &lt;option value="5"&gt;Item5&lt;/option&gt;
  &lt;/select&gt;
  &lt;!-- User must enter the quantity --&gt;
  Quantity: &lt;input type="text" name="stock_plus []" /&gt;&lt;br/&gt;
  &lt;/div&gt; 
  &lt;input type="submit" name="Submit" value="Submit" /&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>the div is repeated 10 times, so the result I should have in my DB table is 10 rows insterted (trans_date, product_id(FK),stock_plus) When echoing Mysql errors, there are none.</p>