the update query just update the last row and affect other with it’s value

You need to change the form so that each row has a hidden input containing the ID of that row.

while($row=mysql_fetch_array($result)){   ?>
<tr>  
    <td><input type="hidden" name="id[]" value="<?php echo $row['id'];  ?>" /></td>
    <td><input type="text" placeholder="First Lecture" name="day[]" value="<?php echo $row['day']; ?>"></td>
    <td><input type="text" placeholder="First Lecture" name="f[]" value="<?php echo $row['first']; ?>"></td>

</tr>
<?php }

Then use that ID in the UPDATE statement, not group_num.

foreach ($_POST['id'] as $i => $id) {
    $scoreaway = $_POST['f'][$i];
    $day = $_POST['day'][$i];
    $query = "UPDATE lectures_table 
                SET first="$scoreaway", day = '$day' 
                WHERE id = $id";
    mysql_query($query);
}

Also, you should switch from the obsolete mysql extension to mysqli or PDO, and use prepared statements. Your current code is vulnerable to SQL injection. See How can I prevent SQL injection in PHP?

Leave a Comment