复选框仅更新数据库中的最后一行

I cant seem to get my checkbox to update only the checked boxes. Right now when I check any box and click submit, it marks the final row with a value of 1 (even if that row isn't checked). What am I missing here in order to post to the same page and it update any boxes that were checked?

while($row = mysql_fetch_array( $result )) {

$addr = $row['address'];
$info = $row['info'];
$date = $row['date'];
$status = $row['status'];
$id = $row['id'];

?>
<tr>
    <td><?php echo $id; ?></td>
    <td><?php echo $addr; ?></td>
    <td><?php echo $info; ?></td> 
    <td><?php echo $date; ?></td>
    <td><?php echo $status; ?></td>
    <td><input type="checkbox" name="handled" value="1"><br></td>
</tr>

<?php
}
?>


</table>

<?php
  if (isset($_POST['checked'])) {
    echo "Posted!";
    $sql2 = "UPDATE requests SET status = 1 WHERE id = '".$id."'";
    mysql_query($sql2) or die(mysql_error());
  }

?>

<br>
<form action="" method="post">
<input name="checked" type="submit"/>

You need to put the id of the row in the checkbox so php knows which rows to update when you check the boxes and submit the form. Also, change the name to handled[] from handled so php knows it's an array of id's.

<input type="checkbox" name="handled[]" value="<?= $id ?>">

Then in your post

<?php
  if (isset($_POST['checked'])) {
    $ids = implode(',',$_POST['handled']);
    $sql2 = "UPDATE requests SET status = 1 WHERE id IN ($ids)";
    // if you also want to update when boxes are unchecked,
    // then run the following query instead
    // UPDATE requests SET status = id IN ($ids)
    // id in ($ids) will evaluate to either a 0 or 1 in mysql
    mysql_query($sql2) or die(mysql_error());
  }
?>

Note: The code above is vulnerable to SQL injection and you should make sure that $ids actually consists of numbers before running the query. Also, mysql_ functions have been deprecated and you should not use them anymore (switch to mysqli_ or PDO).

Your code is vulnerable to sql injection. Use either mysqli or PDO

Update that specific part of your code as following in php and put checkbox inside form.

<?php
  if (isset($_POST)) {
    echo "Posted!";
    //uncomment print_r for debugging
    //print_r($_POST);
    if(isset($_POST['handled'])=='on')
    {
        echo "checkbox is checked for ".$id." so update the status for id";
        $sql2 = "UPDATE requests SET status = 1 WHERE id = '".$id."'";
        //do querying here
    }
    else
    {
        echo "checkbox is not checked for ".$id;
    }

  }

?>

and form like this

<form action="" method="post">
<!-- put checkbox inside the form -->
<input type="checkbox" name="handled"/>
<input name="checked" type="submit"/>

[EDIT]

One thing you have to know about checkbox is unchecked checkboxes are not even posted. Lets say if you have a textbox with no value, on submit the null textbox will be posted, But in case of checkboxes if the checkbox is not marked the post ignores/avoid even the null value, which in this case is treated as it non existing element. Nothing will be posted, not even the null value. To overcome this use a hidden field with checkbox to submit value automatically with the checkbox value in it manupulated with javascript onchange of checkbox checked or not.