title: insert multiple fields using foreach loop in php
body: <p>I have a problem when I want to insert multiple fields into one table.
  it insert only 0 value to each table.</p>
  
  <p>Here's my form:</p>
  
  <pre><code>&lt;table class=table_position&gt;
  &lt;?php
  for($num_request=$_POST['number_item'];$num_request&gt;0;$num_request--){
  
  echo"&lt;tr&gt;
  &lt;th&gt;&lt;input type='number' step='1' min='1' name='quantity[$num_request]' id='quantity$num_request' onkeyup='multiply$num_request();' style='text-align:center;width:96.5px;height:25;' required&gt;&lt;/th&gt;
  &lt;th&gt;&lt;input type='text' name='unit[$num_request]' align='middle' style='text-align:center;width:65px;height:25;' required&gt;&lt;/th&gt;
  &lt;th&gt;&lt;input type='text' name='description[$num_request]' align='middle' style='text-align:center;width:343px;height:25;' required&gt;&lt;/th&gt;
  &lt;th&gt;&lt;input type='text' name='stocknum[$num_request]' align='middle' style='text-align:center;width:99px;height:25;' required&gt;&lt;/th&gt;
  &lt;th&gt;&lt;input type='number' min='1' name='unitcost[$num_request]' id='unitcost$num_request' onkeyup='multiply$num_request(),add() ' align='middle' style='text-align:center;width:100px;height:25;' required&gt;&lt;/th&gt;
  &lt;th&gt;&lt;input type='text'  name='totalcost[$num_request]' id='product$num_request' class='add'  align='middle' style='text-align:center;width:164px;height:25;' readonly&gt;&lt;/th&gt;
  &lt;/tr&gt;";
  }
  ?&gt;
  
  
  &lt;/table&gt;
  </code></pre>
  
  <p>And here's the submit code:`</p>
  
  <pre><code>    $query="SELECT * FROM request WHERE username='$usernames' ORDER BY id DESC LIMIT 1";
  $result=mysqli_query($con,$query);
  $row = mysqli_fetch_array($result);
  $num_row=mysqli_num_rows($result);  
  $request_id=$row['id'];
  
  $i = 0;
  foreach ($_POST as $val) {
  $quantity=$_POST['quantity'][$i];
  $unit=$_POST['unit'][$i];
  $item_description=$_POST['description'][$i];
  $unit_cost=$_POST['unitcost'][$i];
  $total_cost=$_POST['totalcost'][$i];
  
  include("dbc1.php");
  $sql="INSERT INTO request_items (request_id,quantity,unit,item_description,unit_cost,total_cost) 
  VALUES ('$request_id','$quantity','$unit','$item_description','$unit_cost','$total_cost')";
  $i++;
  if(!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  
  }
  </code></pre>
  
  <p>The query inserts into the database, but inserts 0 values to each table.</p>
  
  <p>Can someone please help me?</p>