title: multiple rows are not stored in database
body: <p>I am using jQuery and Ajax in php. I want to submit multiple rows at a time. but my code is not working properly due to the 'plantation_journal_no' which is a primary key of plantation_journal_details and the foreign key of past_history_species_details . I am badly stuck here. Can anyone please advice me that how can I solve this problem?? Thanks in advance guys. codes are given below : </p>
  
  <p>index.php</p>
  
  <pre><code>&lt;div class="row"&gt;
      &lt;form method="post" id="insert_form2" style="padding-right: 10px;"&gt;
          &lt;div class="table-repsonsive"&gt;
              &lt;span id="error"&gt;&lt;/span&gt;
              &lt;table class="table table-bordered" id="item_table2"&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Species&lt;/th&gt;
                      &lt;th&gt;Product&lt;/th&gt;
                      &lt;th&gt;Quantity&lt;/th&gt;
                      &lt;th&gt;Value (Rs.)&lt;/th&gt;
                      &lt;th&gt;&lt;button type="button" name="add2" class="btn btn-success btn-sm add2"&gt;&lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt;&lt;/button&gt;&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/div&gt;
          &lt;br&gt;
          &lt;div align="right"&gt;
              &lt;input  type="submit" name="submit4" class="btn btn-info" value="Add" onclick="move4()"&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;script&gt;
      $(document).ready(function(){
  
       $(document).on('click', '.add2', function(){
        var html = '';
        html += '&lt;tr&gt;';
        html += '&lt;td&gt;&lt;input type="text" name="species[]" class="form-control species" /&gt;&lt;/td&gt;';
        html += '&lt;td&gt;&lt;select name="product[]" class="form-control product"&gt;&lt;option value=""&gt;---Select---&lt;/option&gt;&lt;option value="reserved"&gt;Reserved&lt;/option&gt;&lt;option value="protected"&gt;Protected&lt;/option&gt;&lt;option value="Unclassed"&gt;Unclassed&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;';
        html += '&lt;td&gt;&lt;input type="text" name="quantity[]" class="form-control quantity" /&gt;&lt;/td&gt;';
        html += '&lt;td&gt;&lt;input type="text" name="value[]" class="form-control value" /&gt;&lt;/td&gt;';
        html += '&lt;td&gt;&lt;button type="button" name="remove2" class="btn btn-danger btn-sm remove2"&gt;&lt;span class="glyphicon glyphicon-minus"&gt;&lt;/span&gt;&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;';
        $('#item_table2').append(html);
       });
  
       $(document).on('click', '.remove2', function(){
        $(this).closest('tr').remove();
       });
  
       $('#insert_form2').on('submit', function(event){
        event.preventDefault();
        var error = '';
        $('.species').each(function(){
         var count = 1;
         if($(this).val() == '')
         {
          error += "&lt;p&gt;Enter Item Name at "+count+" Row&lt;/p&gt;";
          return false;
         }
         count = count + 1;
        });
  
        $('.product').each(function(){
         var count = 1;
         if($(this).val() == '')
         {
          error += "&lt;p&gt;Enter Item Quantity at "+count+" Row&lt;/p&gt;";
          return false;
         }
         count = count + 1;
        });
  
        $('.quantity').each(function(){
         var count = 1;
         if($(this).val() == '')
         {
          error += "&lt;p&gt;Select Unit at "+count+" Row&lt;/p&gt;";
          return false;
         }
         count = count + 1;
        });
        $('.value').each(function(){
         var count = 1;
         if($(this).val() == '')
         {
          error += "&lt;p&gt;Select Unit at "+count+" Row&lt;/p&gt;";
          return false;
         }
         count = count + 1;
        });
        var form_data = $(this).serialize();
        if(error == '')
        {
         $.ajax({
          url:"insert2.php",
          method:"POST",
          data:form_data,
          success:function(data)
          {
           if(data == 'ok')
           {
            $('#item_table2').find("tr:gt(0)").remove();
           }
          }
         });
        }
       });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>insert2.php</p>
  
  <pre><code>&lt;?php
  //insert.php;
  
  if(isset($_POST["species"]))
  {
      $connect = new PDO("mysql:host=localhost;dbname=forestdb", "root", "");
      $id=uniqid();
      $sql="SELECT plantation_journal_no from plantation_journal_basic_details 
       where plantation_journal_no=(select max(plantation_journal_no) from 
       plantation_journal_basic_details);";
      $state=$connect-&gt;prepare($sql);
      $state-&gt;execute();
      $row=$state-&gt;fetch();
      $plantation_journal_no = $row['plantation_journal_no'];
      for($count = 0; $count &lt; count($_POST["species"]); $count++)
      {  
          $query = "INSERT INTO past_history_species_details
         (id, plantation_journal_no, species, product, quantity, value) 
         VALUES (:id, :plantation_journal_no, :species, :product, :quantity, 
         :value) ";
          $statement = $connect-&gt;prepare($query);
          $statement-&gt;execute(
              array(
                  ':id'                     =&gt; $id,
                  ':plantation_journal_no'  =&gt; $plantation_journal_no,
                  ':species'                  =&gt; $_POST["species"][$count],
                  ':product'                  =&gt; $_POST["product"][$count], 
                  ':quantity'             =&gt; $_POST["quantity"][$count], 
                  ':value'                   =&gt; $_POST["value"][$count]
                  )
              );
      }
      $result = $statement-&gt;fetchAll();
      if(isset($result))
      {
          echo 'ok';
      }
  }
  ?&gt;
  </code></pre>
  
  <p>Frontend Table:
  <a href="https://i.stack.imgur.com/4lmJX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4lmJX.png" alt="Frontend Table"></a></p>
  
  <p>Backend Table
  <a href="https://i.stack.imgur.com/PrJch.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PrJch.png" alt="Backend Table"></a></p>