azzaxp
azzaxp

Reputation: 717

How do I update the Table for entering marks of students foreach student Id each mark in php mysql

Hi I have senario where i need to update the MySQL table which has student ID and Student Marks. Now the Student ID is unique here. How do I use only one form to update all the students marks.

$result= mysql_query("SELECT fname,usn FROM student where branch='$branch' and section='$section' and semester='$semester'") or die(mysql_error());  
    echo "<form action=\"marks.php\" method=\"POST\">";
    echo "<table border='6' width='500' cellspacing='10' cellpadding='10' style='font-size:14px'>";
     echo "<caption>";
     echo "<b style='font-size:18px'>Internal</b> ";
     echo "<b style='font-size:18px'>";
     echo $internal;
     echo "</b>";
     echo " <b style='font-size:18px'>marks of</b> ";
     echo "<b style='font-size:18px'>";
     echo $subject;
      echo "</b>";
     echo "</caption>";
     echo "<tr><th>USN</th><th>FNAME</th><th>MARKS</th></tr>";

    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
    echo "<tr><td>";
    echo $row['usn'];
    echo "</td><td>";
    echo $row['fname'];
    echo "</td><td>";
    echo "<input name=\"internal\" type=\"text\"  value=\"\" >";
    echo "</td></tr>";
    echo "</table>";

    echo "<input name=\"update\"  id=\"update\"type=\"submit\"  value=\"submit \"align=\"middle\" >";

Upvotes: 0

Views: 1955

Answers (1)

msgmash.com
msgmash.com

Reputation: 1035

Each time you print out the input field for the student mark, you could print a name attribute for the input field that uniquely identifies it - using an integer counter, for example, from 0 to N-1, where N is the number of students. You could also pass a hidden input field with the total number of students. The PHP code that receives the data then uses the hidden input field to loop over the input fields in the form data.

For example, if the input fields end up being named FIELD0 ... FIELD20, then the hidden input field has value 21, so the PHP code simply says this:

$marks = array();
for ($i = 0; $i < $NUMOFMARKS; $i++) {
    $marks[] = $POST['FIELD' . $i];
}

Then build your SQL query from the array of marks.

Upvotes: 1

Related Questions