title: How to insert data to database using multiple array using POST method with ajax:
body: <p>I read similar answer here in this question: <a href="https://stackoverflow.com/questions/18665208/how-to-insert-into-mysql-row-from-multiple-post-arrays">How to insert into MYSQL row from multiple $_POST arrays</a> and <a href="https://stackoverflow.com/questions/18665208/how-to-insert-into-mysql-row-from-multiple-post-arrays">How to insert into MYSQL row from multiple $_POST arrays</a> but the problem is these answers do not work in my code. Is it because im using an ajax? and i only get the value of the first array. </p>
  
  <p>If i also place the variable declaration inside the for loop it is not working too. </p>
  
  <p>Here is my ajax: </p>
  
  <pre><code>var name = []; 
  $('input[name="name[]"]').map(function(){ name.push($(this).val()); }); var studid = []; 
  $('input[name="studid[]"]').map(function(){ studid.push($(this).val()); }); var nameStr = name != '' ? '&amp;name='+ name : '';
  var studStr = studid != '' ? '&amp;studid='+ studid : '';
  var dataString = 'subject='+ subject + '&amp;section=' + section + studStr + nameStr;
  
  $.ajax({ type: "POST", url: 'save.php', data: dataString, dataType: "html", 
  success: function(data) {
      $('input#subject-field').val('');
      $('input#section-field').val('');
      $('input.record-input-forms').val('');
      $('#status-message').css({"color":"#39b1c6"});
      $('#status-message').html('Save successfully',function(){
      $('#status-message').fadeOut(2000); }); }, 
  error:function (xhr, ajaxOptions, thrownError){
      alert(thrownError); } });
      return false;
  }); 
  </code></pre>
  
  <p>Here is my php: </p>
  
  <pre><code>if(isset($_POST['studid']) || isset($_POST['name'])){
      $studid = array_map(mysql_real_escape_string, explode(",",$_POST['studid']));
      $name = array_map(mysql_real_escape_string, explode(",",$_POST['name']));   
  
      for ($i=0; $i&lt;count($studid); $i++){
          $sql_1 = "INSERT INTO tbl_student(StudentID, StudentName, SubjectID)  VALUES ('".$studid[$i]."', '".$name[$i]."', LAST_INSERT_ID())"; 
          mysqli_query($con,$sql_1);  
      } 
  }
  </code></pre>