title: Insert into sql database dynamic form fields added
body: <p>I have the following code, that inserts data into my sql database.  :</p>
  
  <pre><code>&lt;?php
  
  require 'database.php';
  
  if(!empty($_GET['wid']))      { $wid = $_GET['wid'];  }
  elseif(!empty($_POST['wid'])) { $wid = $_POST['wid']; }
  else                         { $wid = null;         }
  
  if ( !empty($_POST)) {
          // keep track validation $
  
          $ModelError       = null;
          $DescriptionError = null;
          $CostError        = null;
          $RetailError      = null;
          $QuantityError    = null;
  
          // keep track post values
  
          $Model        = $_POST['model'];
          $Description  = $_POST['description'];
          $Cost         = $_POST['cost'];
          $Retail       = $_POST['retail'];
          $Quantity     = $_POST['qty'];
  
          // validate input
          $valid = true;
          if (empty($Model)) {
              $ModelError = 'Please enter Model';
              $valid = false;
          }
          if (empty($Description)) {
              $DescriptionError = 'Please enter Description';
              $valid = false;
          }
          if (empty($Cost)) {
              $CostError = 'Please enter Cost';
              $valid = false;
          }
          if (empty($Retail)) {
              $RetailError = 'Please enter Retail';
              $valid = false;
          }
          if (empty($Quantity)) {
              $QuantityError = 'Please enter Quantity';
              $valid = false;
          }
  
  if ($valid) {
              $pdo = Database::connect();
              $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $sql = "INSERT INTO items (WID, model, description, cost, retail, qty) values(?, ?, ?, ?, ?, ?);";
              $q = $pdo-&gt;prepare($sql);
              $q-&gt;execute(array($wid,$Model, $Description, $Cost, $Retail, $Quantity));
  
              Database::disconnect();
              header("Location: index.php");
          }
      }
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;Workorder System&lt;/title&gt;
  
      &lt;!-- Bootstrap core CSS --&gt;
      &lt;link href="css/jquery-ui.min.css" rel="stylesheet"&gt;
      &lt;link href="css/bootstrap.min.css" rel="stylesheet"&gt;
      &lt;link href="css/datepicker.css" rel="stylesheet"&gt;
      &lt;link href="css/style.css" rel="stylesheet"&gt;
  
      &lt;!-- Custom styles for this template --&gt;
      &lt;link href="css/sticky-footer-navbar.css" rel="stylesheet"&gt;
  
      &lt;!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --&gt;
      &lt;!--[if lt IE 9]&gt;
        &lt;script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"&gt;&lt;/script&gt;
        &lt;script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"&gt;&lt;/script&gt;
      &lt;![endif]--&gt;
    &lt;/head&gt;
  
    &lt;body&gt;
  
  
      &lt;!-- Begin page content --&gt;
      &lt;div class="container content"&gt;
          &lt;div class='row'&gt;
              &lt;div class='col-xs-12 col-sm-12 col-md-12 col-lg-12'&gt;
                  &lt;h1 class="text-center title"&gt;Workorder System &lt;/h1&gt;
              &lt;/div&gt;
          &lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
          &lt;div class='row'&gt;
              &lt;div class='col-xs-12 col-sm-12 col-md-12 col-lg-12'&gt;
              &lt;form class="form-horizontal" action="index.php" method="post"&gt;
              &lt;input type="hidden" name="wid" value="&lt;?php echo htmlentities($wid); ?&gt;"&gt;
                  &lt;table class="table table-bordered table-hover"&gt;
                      &lt;thead&gt;
                          &lt;tr&gt;
                              &lt;th width="2%"&gt;&lt;input id="check_all" class="formcontrol" type="checkbox"/&gt;&lt;/th&gt;
                              &lt;th width="15%"&gt;ID&lt;/th&gt;
                              &lt;th width="15%"&gt;Model #&lt;/th&gt;
                              &lt;th width="38%"&gt;Description&lt;/th&gt;
                              &lt;th width="15%"&gt;Cost&lt;/th&gt;
                              &lt;th width="15%"&gt;Retail&lt;/th&gt;
                              &lt;th width="15%"&gt;Quantity&lt;/th&gt;
                          &lt;/tr&gt;
                      &lt;/thead&gt;
                      &lt;tbody&gt;
                          &lt;tr&gt;
                              &lt;td&gt;
                              &lt;input class="case" type="checkbox"/&gt;&lt;/td&gt;
                              &lt;td&gt;
                              &lt;input type="text" data-type="ID" name="idNo[]" id="idNo_1" class="form-control autocomplete_txt" autocomplete="off"&gt;&lt;/td&gt;
                              &lt;td&gt;
  
                              &lt;div class="control-group &lt;?php echo !empty($ModelError)?'error':'';?&gt;"&gt;
                                      &lt;div class="controls"&gt;
                                          &lt;input type="text" data-type="model" name="model[]" id="model_1" placeholder="Model" value="&lt;?php echo !empty($Model)?$Model:'';?&gt;" class="form-control autocomplete_txt" autocomplete="off"&gt;
                                              &lt;?php if (!empty($ModelError)): ?&gt;
                                                  &lt;span class="help-inline"&gt;&lt;?php echo $ModelError;?&gt;&lt;/span&gt;
                                              &lt;?php endif; ?&gt;
                                      &lt;/div&gt;
                              &lt;/div&gt;
  
                              &lt;/td&gt;
                              &lt;td&gt;
  
                              &lt;div class="control-group &lt;?php echo !empty($DescriptionError)?'error':'';?&gt;"&gt;
                                      &lt;div class="controls"&gt;
                                          &lt;input type="text" data-type="description" name="description[]" id="description_1" placeholder="Description" value="&lt;?php echo !empty($Description)?$Description:'';?&gt;" class="form-control autocomplete_txt" autocomplete="off"&gt;
                                              &lt;?php if (!empty($DescriptionError)): ?&gt;
                                                  &lt;span class="help-inline"&gt;&lt;?php echo $DescriptionError;?&gt;&lt;/span&gt;
                                              &lt;?php endif; ?&gt;
                                      &lt;/div&gt;
                              &lt;/div&gt;
  
                              &lt;/td&gt;   
                              &lt;td&gt;
  
                              &lt;div class="control-group &lt;?php echo !empty($CostError)?'error':'';?&gt;"&gt;
                                      &lt;div class="controls"&gt;
                                          &lt;input type="text" data-type="cost" name="cost[]" id="cost_1" placeholder="Cost" value="&lt;?php echo !empty($Cost)?$Cost:'';?&gt;" class="form-control autocomplete_txt" autocomplete="off"&gt;
                                              &lt;?php if (!empty($CostError)): ?&gt;
                                                  &lt;span class="help-inline"&gt;&lt;?php echo $CostError;?&gt;&lt;/span&gt;
                                              &lt;?php endif; ?&gt;
                                      &lt;/div&gt;
                              &lt;/div&gt;
  
                              &lt;/td&gt;
                              &lt;td&gt;
  
                              &lt;div class="control-group &lt;?php echo !empty($RetailError)?'error':'';?&gt;"&gt;
                                      &lt;div class="controls"&gt;
                              &lt;input type="text" data-type="retail" name="retail[]" id="retail_1" placeholder="Retail" value="&lt;?php echo !empty($Retail)?$Retail:'';?&gt;" class="form-control autocomplete_txt" autocomplete="off"&gt;
                              &lt;?php if (!empty($RetailError)): ?&gt;
                                                  &lt;span class="help-inline"&gt;&lt;?php echo $RetailError;?&gt;&lt;/span&gt;
                                              &lt;?php endif; ?&gt;
                                      &lt;/div&gt;
                              &lt;/div&gt;
  
                              &lt;/td&gt;
                              &lt;td&gt;
  
                              &lt;div class="control-group &lt;?php echo !empty($QuantityError)?'error':'';?&gt;"&gt;
                                      &lt;div class="controls"&gt;
                              &lt;input type="number" name="quantity[]" id="quantity_1" placeholder="Quantity" value="&lt;?php echo !empty($Quantity)?$Quantity:'';?&gt;" class="form-control autocomplete_txt" autocomplete="off"&gt;
                              &lt;?php if (!empty($QuantityError)): ?&gt;
                                                  &lt;span class="help-inline"&gt;&lt;?php echo $QuantityError;?&gt;&lt;/span&gt;
                                              &lt;?php endif; ?&gt;
                                      &lt;/div&gt;
                              &lt;/div&gt;
  
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/tbody&gt;
                  &lt;/table&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class='row'&gt;
              &lt;div class='col-xs-12 col-sm-3 col-md-3 col-lg-3'&gt;
                  &lt;button class="btn btn-danger delete" type="button"&gt;- Delete&lt;/button&gt;
                  &lt;button class="btn btn-success addmore" type="button" id="Add"&gt;+ Add More&lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;h2&gt;Notes: &lt;/h2&gt;
          &lt;div class='row'&gt;
              &lt;div class='col-xs-12 col-sm-12 col-md-12 col-lg-12'&gt;
                  &lt;div class="form-group"&gt;
                      &lt;textarea class="form-control" rows='5' id="notes" placeholder="Your Notes"&gt;&lt;/textarea&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;      
          &lt;div class="form-actions"&gt;
                            &lt;button type="submit" class="btn btn-success"&gt;Create&lt;/button&gt;
                            &lt;a class="btn" href="index.php"&gt;Back&lt;/a&gt;
                          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;script src="js/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="js/jquery-ui.min.js"&gt;&lt;/script&gt;
      &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
      &lt;script src="js/bootstrap-datepicker.js"&gt;&lt;/script&gt;
      &lt;script src="js/auto.js"&gt;&lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This code worked perfectly before it was altered.  It used to only insert rows into the database one at a time, before I added this code :</p>
  
  <pre><code>&lt;div class='row'&gt;
              &lt;div class='col-xs-12 col-sm-3 col-md-3 col-lg-3'&gt;
                  &lt;button class="btn btn-danger delete" type="button"&gt;- Delete&lt;/button&gt;
                  &lt;button class="btn btn-success addmore" type="button" id="Add"&gt;+ Add More&lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>and the ajax backend :</p>
  
  <pre><code>//adds extra table rows
  var i=$('table tr').length;
  $(".addmore").on('click',function(){
      html = '&lt;tr&gt;';
      html += '&lt;td&gt;&lt;input class="case" type="checkbox"/&gt;&lt;/td&gt;';
      html += '&lt;td&gt;&lt;input type="text" data-type="ID" name="idNo[]" id="idNo_'+i+'" class="form-control autocomplete_txt" autocomplete="off"&gt;&lt;/td&gt;';
      html += '&lt;td&gt;&lt;input type="text" data-type="model" name="model[]" id="model_'+i+'" class="form-control autocomplete_txt" autocomplete="off"&gt;&lt;/td&gt;';
      html += '&lt;td&gt;&lt;input type="text" data-type="description" name="description[]" id="description_'+i+'" class="form-control autocomplete_txt" autocomplete="off"&gt;&lt;/td&gt;';
      html += '&lt;td&gt;&lt;input type="text" data-type="cost" name="cost[]" id="cost_'+i+'" class="form-control autocomplete_txt" autocomplete="off"&gt;&lt;/td&gt;';
      html += '&lt;td&gt;&lt;input type="text" data-type="retail" name="retail[]" id="retail_'+i+'" class="form-control autocomplete_txt" autocomplete="off"&gt;&lt;/td&gt;';
      html += '&lt;td&gt;&lt;input type="text" data-type="quantity" name="quantity[]" id="quantity_'+i+'" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"&gt;&lt;/td&gt;';
      html += '&lt;/tr&gt;';
      $('table').append(html);
      i++;
  });
  
  //to check all checkboxes
  $(document).on('change','#check_all',function(){
      $('input[class=case]:checkbox').prop("checked", $(this).is(':checked'));
  });
  
  //deletes the selected table rows
  $(".delete").on('click', function() {
      $('.case:checkbox:checked').parents("tr").remove();
      $('#check_all').prop("checked", false); 
      calculateTotal();
  });
  
  //autocomplete script
  $(document).on('focus','.autocomplete_txt',function(){
      type = $(this).data('type');
  
      if(type =='ID' )autoTypeNo=0;
      if(type =='model' )autoTypeNo=1;    
  
      $(this).autocomplete({
          source: function( request, response ) {
              $.ajax({
                  url : 'ajax.php',
                  dataType: "json",
                  method: 'post',
                  data: {
                     name_startsWith: request.term,
                     type: type
                  },
                   success: function( data ) {
                       response( $.map( data, function( item ) {
                          var code = item.split("|");
                          return {
                              label: code[autoTypeNo],
                              value: code[autoTypeNo],
                              data : item
                          }
                      }));
                  }
              });
          },
          autoFocus: true,            
          minLength: 0,
          select: function( event, ui ) {
              var names = ui.item.data.split("|");                        
              id_arr = $(this).attr('id');
              id = id_arr.split("_");
              $('#idNo_'+id[1]).val(names[0]);
              $('#model_'+id[1]).val(names[1]);
              $('#description_'+id[1]).val(names[2]);
              $('#cost_'+id[1]).val(names[3]);
              $('#retail_'+id[1]).val(names[4]);
              $('#quantity_'+id[1]).val(1);
              $('#total_'+id[1]).val( 1*names[4] );
              calculateTotal();
          }               
      });
  });
  
  //It restrict the non-numbers
  var specialKeys = new Array();
  specialKeys.push(8,46); //Backspace
  function IsNumeric(e) {
      var keyCode = e.which ? e.which : e.keyCode;
      console.log( keyCode );
      var ret = ((keyCode &gt;= 48 &amp;&amp; keyCode &lt;= 57) || specialKeys.indexOf(keyCode) != -1);
      return ret;
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&lt;?php
  
  require_once 'config.php';
  if(!empty($_POST['type'])){
      $type = $_POST['type'];
      $name = $_POST['name_startsWith'];
      $query = "SELECT ID, model, category, subcategory, description, cost, retail FROM products where UPPER($type) LIKE '".strtoupper($name)."%'";
      $result = mysqli_query($con, $query);
      $data = array();
      while ($row = mysqli_fetch_assoc($result)) {
          $name = $row['ID'].'|'.$row['model'].'|'.$row['description'].'|'.$row['cost'].'|'.$row['retail'];
          array_push($data, $name);
      }   
      echo json_encode($data);exit;
  }
  </code></pre>
  
  <p>Now, I got help with the ajax section of the code, which works(adds the form fields properly), and the POST results work fine(screened firefox's web developer tab, and it posts all of my form field data properly), the only thing that I am having issues with is how to translate MULTIPLE submissions at a time.  Apparently the sql code that I have working with one submission does not work with multiple consecutive submissions, and I get the following error :</p>
  
  <blockquote>
    <p>Notice: Array to string conversion in C:\wamp\www\invoice\index.php on line 131 Call Stack #TimeMemoryFunctionLocation 10.0012276928{main}( )..\index.php:0 Array" class="form-control autocomplete_txt" autocomplete="off"> </p>
  </blockquote>
  
  <p>and for some odd reason :</p>
  
  <blockquote>
    <p>Notice: Undefined index: qty in C:\wamp\www\invoice\index.php on line 24</p>
  </blockquote>
  
  <p>Any help with this matter would be GREATLY appreciated, have been troubleshooting for over a week now :P</p>