title: how to insert html table values in mysql table
body: <p>Using following code I have fetched <code>Student ID</code> and <code>Student name</code> from mysql table in html table. In third column which is <code>Obtained Marks</code> I am getting student marks through input box. Now I want to insert all three columns (Student ID, Student Name and Obtained Marks) in new table which is testrecord.</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  &lt;?php
  $connection = mysqli_connect ('localhost', 'admin', 'password', 'db');
  if (!$connection) {
      echo 'Not connected to server';
  }
  
  $select_db = mysqli_select_db($connection, 'db');
  if (!$select_db) {
      echo 'Not connected to database';
  }
  
  $SelectClass = $_POST ['selectclass'];
  $sql= "SELECT * FROM students WHERE class = '$SelectClass'";
  $query = mysqli_query($connection, $sql);
  if (!$query) {
      die ('SQL Error: ' . mysqli_error($connection));
  }
  
  mysqli_close($connection);
  ?&gt;
   &lt;body&gt;
      &lt;div class="container"&gt;
      &lt;form class="well form-horizontal" action="insert_marks.php" method="post"&gt;
      &lt;h1&gt;&lt;strong&gt;Please enter marks of each student for subject&lt;/strong&gt;&lt;/h1&gt;
      &lt;form action="" method="post"&gt;
      &lt;table id = "result" class="data-table"&gt;
          &lt;caption class="title"&gt;&lt;/caption&gt;
          &lt;thead&gt;
              &lt;tr&gt;    
                  &lt;th&gt;Sr.No.&lt;/th&gt;
                  &lt;th&gt;Student ID&lt;/th&gt;
                  &lt;th&gt;Student Name&lt;/th&gt;
                  &lt;th&gt;Marks Obtained&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
          &lt;?php
          $no     = 1;
          $total  = 0;
          while ($row = mysqli_fetch_array($query)) {
              $stu  = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
              echo '&lt;tr&gt;
                      &lt;td&gt;'.$no.'&lt;/td&gt;
                      &lt;td&gt;'.$row['student_id'].'&lt;/td&gt;
                      &lt;input type="hidden" name="student_id" value='.$row['student_id'].'&gt;
                      &lt;td&gt;'.$row['student_name'].'&lt;/td&gt;
                      &lt;input type="hidden" name="student_name" value='.$row['student_name'].'&gt;
                      &lt;td&gt;
                          &lt;div class="search-block clearfix"&gt;
                              &lt;input name="obtmarks" placeholder="" type="number"&gt;
                          &lt;/div&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;';
              $total += $row['stu_id'];
              $no++;
          }
          ?&gt;
          &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;button type="submit" class="btn btn-warning" value="insert" align="right"&gt;Update&lt;span class="glyphicon glyphicon-send"&gt;&lt;/span&gt;&lt;/button&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>insert_marks.php:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  &lt;?php
  $connection = mysqli_connect ('localhost', 'admin', 'password', 'db');
  if (!$connection) {
      echo 'Not connected to server';
  }
  
  $select_db = mysqli_select_db($connection, 'db');
  if (!$select_db) {
      echo 'Not connected to database';
  }
  //***********Form Submit Goes Here***********//
  while 
  if($_POST) {
      $student_id     =   $_POST['student_id'];
      $student_name   =   $_POST['student_name'];
      $student_marks  =   $_POST['obtmarks'];
  
      $sql= "INSERT INTO testrecord (student_id,student_name,obtained_marks) VALUES ('$student_id','$student_name','$student_marks')";
      if (mysqli_query($connection, $sql)) {
      echo "Marks added successfully.";
      echo "&lt;br&gt;";
      echo "&lt;br&gt;";
      } else {
          echo "Error: " . $sql . "&lt;br&gt;" . mysqli_error($connection);
      }
  }
  mysqli_close($connection);
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>There are 20 entries in table. After inserting marks for each student in text box, above coding inserts only last record in mysql table 'testrecord'. Can you please correct the insert_marks.php code. </p>