title: Inserting multiple generated student records from an HTML5 form into MySQL database
body: <p>I want to insert multiple student records that I have generated in an array into my terminal_assessment table in my database. after entering all the generated student records when I post only the last student records are inserted. below is my code:</p>
  
  <pre><code>&lt;?php
  require_once('login.php');
  
  echo "&lt;form method='post' action=''&gt;";
  
  //fetching all JSS 1 students from students' table
  $student_sql = "SELECT * FROM `sch`.student where classAdmitted = 'jss1' ORDER BY firstName";
  
  //creating table columns for all student records heading
  echo "&lt;table&gt;&lt;tr&gt;&lt;th&gt;S/No.&lt;/th&gt;&lt;th&gt;Admission No&lt;/th&gt;&lt;th&gt;Full Names&lt;/th&gt;&lt;th&gt;Current Class&lt;/th&gt;&lt;th&gt;Subject&lt;/th&gt;&lt;th&gt;CA1&lt;/th&gt;&lt;th&gt;CA2&lt;/th&gt;&lt;th&gt;Exam&lt;/th&gt;&lt;th&gt;Total&lt;/th&gt;&lt;/tr&gt;";
  
  //looping through all the student present in a class and their subjects for exam recording
  foreach($conn-&gt;query($student_sql) as $row1)
  {
      $fullName=strtoupper($row1['firstName']." ".$row1['middleName']." ".$row1['surName']);
      $stud_id = $row1['studentID'];
  
      echo "&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;".$row1['studentID']."&lt;/td&gt;&lt;td&gt;".$fullName."&lt;/td&gt;&lt;td&gt;".$row1['classAdmitted']."&lt;/td&gt;&lt;td class='scol'&gt;&lt;select name='subCode'&gt;&lt;option value='agric'&gt;Agric Science&lt;/option&gt;&lt;option value='bio'&gt;Biology&lt;/option&gt;&lt;option value='bus'&gt;Business Study&lt;/option&gt;&lt;option value='crk'&gt;Christian Religious Study&lt;/option&gt;&lt;option value='econs'&gt;Economics&lt;/option&gt;&lt;option value='eng'&gt;English Language&lt;/option&gt;&lt;option value='fst'&gt;Food Science&lt;/option&gt;&lt;option value='intech'&gt;Introductory Technology&lt;/option&gt;&lt;option value='maths'&gt;Mathematics&lt;/option&gt;&lt;option value='soc'&gt;Social Study&lt;/option&gt;&lt;/select&gt;&lt;/td&gt;&lt;td class='scol'&gt;&lt;input name='ca1' type='number' class='sub' value='0'&gt;&lt;/td&gt;&lt;td class='scol'&gt;&lt;input name='ca2' type='number' class='sub' value='0'&gt;&lt;/td&gt;&lt;td class='scol'&gt;&lt;input type='number' name='ca3' class='sub' value='0'&gt;&lt;/td&gt;&lt;td class='scol'&gt;&lt;input type='number' name='ca4' class='sub' value='0'&gt;&lt;/td&gt;&lt;/tr&gt;";
  }
      echo "&lt;/tr&gt;&lt;/table&gt;";
      echo "&lt;div id='btmbar'&gt;";
      printf('Total Number of Students: %d', $conn-&gt;affected_rows);
      echo "&lt;br&gt;&lt;input id='btn1' type='submit' name='add' value='Get TOTAL'&gt;&lt;input id='btn2' type='submit' value='Submit Student Records' name='submit'&gt;";
      echo "&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;";
  
  $student_ta_id = $sess = $className = $term = $subNames = $ca1 = $ca2 = $ca3 = $ca4 = "";
  
  if(isset($_POST['submit']))
  {   
      $student_ta_id = "$stud_id"."$session"."$className"."$term"."$subjects";
      $term = $_POST['term'];
      $student_ID = $row1['studentID'];
      $className = $row1['classAdmitted'];
      $subjects = $_POST['subCode'];
      $ca1 = $_POST['ca1'];
      $ca2 = $_POST['ca2'];
      $ca3 = $_POST['ca3'];
      $ca4 = $ca1 + $ca2 + $ca3;
  
  $sql = "INSERT INTO `sch`.terminal_assessment (student_ta_id, session, className, term, subjectNames, ca1, ca2, exam, total) VALUES('$student_ta_id', '$session', '$className', '$term', '$subjects', '$ca1', '$ca2', '$ca3', '$ca4')";
  $insert = $conn-&gt;query($sql);
  
  if($insert === True)
  {
      echo "Record inserted successfully". "&lt;br&gt;";
  }
  else
  {
      echo "Error Unable to Insert Record".die($conn-&gt;error);
  }
  
  }
      $conn-&gt;close();
  ?&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/2pgGM.png" rel="nofollow noreferrer">Preview of the HTML5 format</a></p>