title: For loop inserts only 1 MySQL record
body: <p>I have been working on a form that allows entering a menu item for a cafe establishment, alongside its ingredients (arrays of data). Unfortunately, a problem came up as it only executes 1 query even though 2 or more ingredients were entered in the form (dynamic, jQuery).</p>
  
  <p>Here is the PHP code:</p>
  
  <pre><code>&lt;?php
  include("session.php");
  
  if (isset($_POST['submit'])) {
      $productname = $_POST['product_name'];
      $categoryID = $_POST['categoryID'];
      $price = $_POST['srp'];
  
      // ingredients
      $ingredients = $_POST['ingredients'];
      $qty = $_POST['qty'];
      $measure = $_POST['measure'];
  
      if (!empty($productname) &amp;&amp; !empty($categoryID) &amp;&amp; !empty($price) &amp;&amp; !empty($ingredients) &amp;&amp; !empty($qty) &amp;&amp; !empty($measure)) {
          for ($i=0; $i &lt; count($ingredients); $i++) {
              if ($ingredients[$i] != "" &amp;&amp; $qty[$i] != "" &amp;&amp; $measure[$i] != "") {
                  mysqli_query($db, "insert into individual_ingredients values ('', '$productname', '{$ingredients[$i]}', '{$qty[$i]}', '{$measure[$i]}')");
              }
          }
          mysqli_query($db, "insert into end_products values ('', '$productname', '$price', '', '$categoryID')");
          mysqli_query($db, "insert into audit_trail values ('', now(), '{$_SESSION['login_user']}', 'New end product added')");
          header("location: end_products.php");
      } else {
          echo '&lt;font color="red"&gt;'."Incomplete data entered".'&lt;/font&gt;';
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>And here is the HTML form and JQuery:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;&lt;?php echo $login_session; ?&gt; | New End Product Record&lt;/title&gt;
  &lt;link rel="stylesheet" href="css/main.css" type="text/css"&gt;
  &lt;link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"&gt;
  &lt;!-- jQuery library --&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;!-- Bootstrap js library --&gt;
  &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function(){
  //add more fields group
    $(".addMore").click(function(){
          var fieldHTML = '&lt;div class="form-group fieldGroup"&gt;'+$(".fieldGroupCopy").html()+'&lt;/div&gt;';
          $('body').find('.fieldGroup:last').after(fieldHTML);
    });
    //remove fields group
    $("body").on("click",".remove",function(){
        $(this).parents(".fieldGroup").remove();
    });
  });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  </code></pre>
  
  <p>HTML form:</p>
  
  <pre><code>&lt;table style="margin: 5% -1% 0 -10%; font-size: 0.9em"&gt;
  &lt;tr&gt;
  &lt;form action="new_end_product_record.php" method="post"&gt;
  &lt;td&gt;Name&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="product_name"&gt;&lt;/td&gt;
  &lt;td&gt;Raw Material/s Used&lt;/td&gt;
  &lt;td&gt;
  &lt;div class="fieldGroup"&gt;
  &lt;select name="ingredients[]"&gt;
  &lt;option value=""&gt;Ingredient&lt;/option&gt;
  
  &lt;?php
  $items_sql = "select name from raw_materials where status='Active'";
  $get_items = mysqli_query($db, $items_sql);
  
  while ($option = mysqli_fetch_assoc($get_items)) { ?&gt;
  &lt;option value="&lt;?php echo $option['name']; ?&gt;"&gt;&lt;?php echo $option['name']; ?&gt;&lt;/option&gt;
  &lt;?php } ?&gt;
  &lt;/select&gt;
  
  &lt;input type="text" name="qty[]" placeholder="Quantity" style="width:60px"&gt;
  
  &lt;select name="measure[]"&gt;
  &lt;option value=""&gt;Measure Unit&lt;/option&gt;
  &lt;?php
  $get_units = "select * from raw_material_measures";
  $units = mysqli_query($db, $get_units);
  while ($unit = mysqli_fetch_assoc($units)) {
  ?&gt;
  &lt;option value="&lt;?php echo $unit['full_name']; ?&gt;"&gt;&lt;?php echo $unit['full_name']; ?&gt;&lt;/option&gt;
  &lt;?php } ?&gt;
  &lt;/select&gt;
  
  &lt;a href="javascript:void(0)" class="addMore"&gt;ADD&lt;/a&gt;
  &lt;br /&gt;&lt;br /&gt;
  &lt;/div&gt;
  
  &lt;!-- second set --&gt;
  &lt;div class="fieldGroupCopy" style="display: none;"&gt;
  &lt;div class="input-group"&gt;
  &lt;select name="ingredients[]"&gt;
  &lt;option value=""&gt;Ingredient&lt;/option&gt;
  &lt;?php
  $items_sql = "select name from raw_materials where status='Active'";
  $get_items = mysqli_query($db, $items_sql);
  
  while ($option = mysqli_fetch_assoc($get_items)) { ?&gt;
  &lt;option value="&lt;?php echo $option['name']; ?&gt;"&gt;&lt;?php echo $option['name']; ?&gt;&lt;/option&gt;
  &lt;?php } ?&gt;
  &lt;/select&gt;
  
  &lt;input type="text" name="qty[]" placeholder="Quantity" style="width:60px"&gt;
  
  &lt;select name="measure[]"&gt;
  &lt;option value=""&gt;Measure Unit&lt;/option&gt;
  &lt;?php
  $get_units = "select * from raw_material_measures";
  $units = mysqli_query($db, $get_units);
  while ($unit = mysqli_fetch_assoc($units)) {
  ?&gt;
  &lt;option value="&lt;?php echo $unit['full_name']; ?&gt;"&gt;&lt;?php echo $unit['full_name']; ?&gt;&lt;/option&gt;
  &lt;?php } ?&gt;
  &lt;/select&gt;
  
  &lt;a href="javascript:void(0)" class="remove"&gt;REMOVE&lt;/a&gt;
  &lt;br /&gt;&lt;br /&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;SRP&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="srp"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Category&lt;/td&gt;
  &lt;td&gt;
  &lt;select name="categoryID"&gt;
  &lt;option value=""&gt;Select category...&lt;/option&gt;
  &lt;!--list all categories in the database--&gt;
  &lt;?php
  $cat_query = "select category_ID, name from end_products_categories";
  $get_cats = mysqli_query($db, $cat_query);
  
  while ($option = mysqli_fetch_assoc($get_cats)) { ?&gt;
  &lt;option value="&lt;?php echo $option['category_ID']; ?&gt;"&gt;&lt;?php echo $option['name']?&gt;&lt;/option&gt;
  &lt;?php } ?&gt;
  &lt;/select&gt;
  &lt;/td&gt;
  &lt;!-- &lt;td&gt;Expiration&lt;/td&gt;
  &lt;td&gt;&lt;input type="date"&gt;&lt;/input&gt;&lt;/td&gt; --&gt;
  &lt;/tr&gt;
  &lt;/table&gt;&lt;br&gt;
  &lt;input type="submit" class="button" name="submit" value="ADD RECORD"&gt;
  &lt;input type="reset" value="ERASE ALL"&gt;
  &lt;/div&gt;&lt;/form&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Is there a problem with the loop or with the HTML form that prevents the second to the last set of values from being inserted? Any help would be appreciated.</p>