如何在while循环中从动态生成的文本框中发布多个条目

please i am currently working on a school result computation project in php. I have a page that gets all the students that enrolled for a particular subject. A while loop generates three textboxes against each student's name(CA1, CA2, and Exam). Please can anyone help me with a solution?? When the form is submitted only the last entry enters the database. Below is my code.

<?php
session_start();
include 'header_staff.php';

$subject=$_POST['subject'];
$user_id=$_SESSION['user_id'];
$get=mysql_query("SELECT * FROM staffrecord WHERE user_id='$user_id'",$conn);
$go=mysql_fetch_array($get);
$class=$go['class_id'];
$sql=mysql_query("SELECT * FROM student_enrollment WHERE class_id='$class' AND subject_id='$subject'",$conn);
echo '<form action="submitrslt.php" method="post">';
while($subj=mysql_fetch_array($sql)){
echo $subj['name'];
echo '<input type="text" name="ca1" />';
echo '<input type="text" name="ca2" />';
echo '<input type="text" name="exam" /><br><br>';
}
echo '<input type="submit" value="submit" />';

?>

The solution to your problem is to use your input arrays by simply adding [] to the input names:

while($subj=mysql_fetch_array($sql)){
  echo $subj['name'];
  echo '<input type="text" name="ca1[]" />';
  echo '<input type="text" name="ca2[]" />';
  echo '<input type="text" name="exam[]" /><br><br>';
}

Further, you can use the primary key of your table as the index in each iteration (let's suppose this would be $subj['id'], but if it is more meaningful in your case, you can also use $subj['name'] as the index):

while($subj=mysql_fetch_array($sql)){
  echo $subj['name'];
  echo '<input type="text" name="ca1[<?php echo $subj['id'] ?>]" />';
  echo '<input type="text" name="ca2[<?php echo $subj['id'] ?>]" />';
  echo '<input type="text" name="exam[<?php echo $subj['id'] ?>]" /><br><br>';
}

This way you can easily identify values from the resulting array after the post.

PS: Be sure you never trust user input without verifying or sanitizing it - make sure $_POST['subject'] is numeric. You can find plenty of useful tips on how to achieve this.

I have added comments, this will solve your problem i am assuming your subjects data is unique for the loop. And i have defined the input with the subject name which make it unique. Just for an example you can try your own method. This will output all the post variables on your action(form post) screen.

<?php

while($subj=mysql_fetch_array($sql)){
$subname = str_replace(' ','', $subj['name']); //just to replace the space
echo $subj['name'];
echo '<input type="text" name="'.$subname.'_ca1" />'; //to make the field unique for the input entries
echo '<input type="text" name="'.$subname.'_ca2" />';
echo '<input type="text" name="'.$subname.'_exam" /><br><br>';
}
echo '<input type="submit" value="submit" />';
echo '</form>';
?>

Firstly, I would suggest changing the connection to mysqli, which follows a very similar syntax to mysql except that it is not deprecated.

Secondly, the form looks fine, except that the textboxes and submit button don't have any unique identifiers, you might try putting in a unique 'id' for each of the input fields. Since your loop is combining all input fields in one form, this is likely the culprit. Furthermore, I don't see a closing tag for form, i.e. </form>.

Finally, you should look up filter_input for handling superglobals such as $_POST[].

I assume that all fields are meant to be submitted at once rather than individually?