title: How to prevent foreach from duplicating entries from an HTML table array
body: <p>I am trying to add multiple entries gathered from a HTML array table to the database but the value of the quantity seems to be duplicating, including the value for remarks. The whole entry seems to be duplicating as well. How can I prevent them from happening?</p>
  
  <p>Here's my PHP code for inserting the table to the database:</p>
  
  <pre><code>&lt;?php
      $tmp = 0;
      if (isset($_POST['submit'])) {
          if(!empty($_POST["inRemarks"])) {
              foreach($_POST["inRemarks"] as $inRemarks) if ($tmp++ &lt; 2) {
                  $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                  $inQty = $_POST['incQty'];
                  foreach ($inQty as $key =&gt; $incQty) {
                      $prod = $_POST['prodItem']; 
                      foreach($prod as $prodItem) {
                          $emp = $_POST['emp'];
                          $emp1 = $conn-&gt;query("SELECT empID AS empA FROM employee WHERE empName = '$emp'");
                          $emp2 = $emp1-&gt;fetch(PDO::FETCH_ASSOC);
                          $emp3 = $emp2['empA'];
                          $prod1 = $conn-&gt;query("SELECT prodID AS prodA FROM product WHERE prodName = '$prodItem'");
                          $prod2 = $prod1-&gt;fetch(PDO::FETCH_ASSOC);
                          $prod3 = $prod2['prodA'];
                          $sql = "INSERT INTO incoming (inQty, inDate, receiptNo, inRemarks, empID, prodID)
                          VALUES ('$incQty',CURDATE(),'".$_POST['rcno']."','$inRemarks','$emp3','$prod3')";
                          $result = $conn-&gt;query($sql); 
                          echo "&lt;meta http-equiv='refresh' content='0'&gt;";
                      }
                  }
              }
          }
      }
  ?&gt;
  </code></pre>
  
  <p>Here's my JavaScript code for the form and table:</p>
  
  <pre><code>function validateForm() {
      if(document.getElementById('addRcpt').value == "") {
          alert('Please Enter Receipt Number');
          document.getElementById('addRcpt').style.borderColor = "red";
          return false;
      }
      if (document.getElementById('addQty').value == "") {
          alert('Please Enter Quantity');
          document.getElementById('addQty').style.borderColor = "red";
          return false;
      }
      if(confirm('Are you sure you want to add this entry?')) {
          alert("Incoming Product Successfully Added");
          return true;        
      }
      else {
          return false;       
      }
  }
  
  function deleteRow(tableID) {
      try {
          var table = document.getElementById(tableID);
          var rowCount = table.rows.length;
          var current = '';
          for(var i=0; i&lt;rowCount; i++) {
              var row = table.rows[i];
              var chkbox = row.cells[0].childNodes[0];
              if(null != chkbox &amp;&amp; true == chkbox.checked) {
                  if(rowCount &lt;= 1) {
                      alert("Cannot delete all the rows.");
                      break;
                  }
  
                  table.deleteRow(i);
                  rowCount--;
                  i--;
                regroup(i,rowCount,tableID);
              }
          }
      }catch(e) {
          alert(e);
      }
  }
  
  function addRow(tableID) {
      var table = document.getElementById(tableID);
      var rowCount = table.rows.length;
      var row = table.insertRow(rowCount);
      var colCount = table.rows[0].cells.length;
      for(var i=0; i&lt;colCount; i++) {
          var newcell = row.insertCell(i);
          if(i==1){
              newcell.innerHTML = (rowCount+1)
          }
          else{
              newcell.innerHTML = table.rows[0].cells[i].innerHTML;
          }
          switch(newcell.childNodes[0].type) {
              case "text":
              newcell.childNodes[0].value="";
              break;
              case "checkbox":
              newcell.childNodes[0].checked = false;
              break;
              case "select-one":
              newcell.childNodes[0].selectedIndex = 0;
              break;
          }
      }
  }
  
  function regroup(i,rc,ti){
      for(j = (i+1);j&lt;rc;j++){
          document.getElementById(ti).rows[j].cells[1].innerHTML = j+1;
      }
  }
  </code></pre>
  
  <p>Query result:</p>
  
  <p><a href="https://i.stack.imgur.com/yxkWg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yxkWg.png" alt="Query result"></a></p>