Select a tag with an empty text box

advertisements

I have done a form which retrieves data from the database. This form allows you to display the list of students from the database but the problem is I cannot correctly input their scores individually. It does allow to insert scores but unfortunately, it only accepts the last score from the last student and inputs the same score to all students.

These are my codes:

FORM code

if ($result->num_rows > 0) { // output data of each row
   while($row = $result->fetch_assoc()) {
    if($row['status']=='p'){

    <form name="result" method="post">
    <?php {          //this form will display the set of students

            echo $row['lastname'] . ', ' . $row['firstname'];
            echo '<input type="hidden" name="selected[]" value="'.$row['reviewee_idnumber'].'"/>'; ?>
            <input type="text" name="score" required="required" size="20" placeholder="Score"/><br><br>
    <?php   echo '</br>';
        }

    } //if statement
    } //while statement
    ?>

<input type="submit" name="submit" value="Submit"/>
<input type="hidden" name="code" value="<?php echo $code;?>"/>
<input type="hidden" name="subject" value="<?php echo $subject;?>"/>
<input type="hidden" name="items" value="<?php echo $items;?>"/>
<input type="hidden" name="date" value="<?php echo $date;?>"/>
</form>

This is where I insert my code into the database

if(isset($_POST['submit'])){

$code = $_POST['code'];
$subject = $_POST['subject'];
$items = $_POST['items'];
$date = $_POST['date'];
$score = $_POST['score']; //get score

$update = mysql_query("INSERT INTO exam (exam_code, subject, date, total_item)
                                    VALUE ('$code','$subject', '$date', '$items')"); 

if(!empty($_POST['selected'])) {
    $checked_count = count($_POST['selected']);// Counting number of checked checkboxes.
    //echo "You have selected following ".$checked_count." option(s): <br/>"; 

    foreach($_POST['selected'] as $selected){  // Loop to store and display values of individual inputs.
         $updatedata="INSERT INTO result (score, exam_code, reviewee_idnumber)
                                    VALUE ('$score', '$code', '$selected')";

            if(@mysql_query($updatedata,$dbc)){
                print '<p> successful!</p>';
            }else{
                print '<p> failed. '.mysql_error().'</p>';
            }

It should display list students (which is fine) and right beside their name is a blank space that will accept their score (which doesn't work).


Because <form name="result" method="post"> is inside the while loop, you actually have multiple forms in your page. The submit button acts only on the last form, that's why only the last student's score gets updated.

Also, because there are multiple input elements with the same name, only a single value is sent in the POST request. A solution for this would be to include the student's ID in the name:

name="score'.$row['reviewee_idnumber'].'"

You need to update the form processing logic accordingly.