1

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"/>
Taylor Reed
  • 335
  • 2
  • 6
  • 18
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Sep 25 '15 at 20:31
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Sep 25 '15 at 20:31
  • Can't understand what you are asking – The Guest Sep 25 '15 at 20:32
  • Change your name on the input to "handled[]" and you will get an array of all the checked boxes. – Tom Sep 25 '15 at 20:33
  • `$id` will have the value of the last row of the table, and that's what you're updating. what are you trying to do? – FuzzyTree Sep 25 '15 at 20:36
  • Put the update in the `while` loop. – chris85 Sep 25 '15 at 20:39

2 Answers2

0

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).

FuzzyTree
  • 32,014
  • 3
  • 54
  • 85
0

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.

aimme
  • 6,385
  • 7
  • 48
  • 65
  • Posted!Array ( [checked] => Submit [address] => [info] => ) checkbox is not checked for 12 12 is the last id in the rows, I did not check that box though – Taylor Reed Sep 25 '15 at 20:58
  • i am not clear..do you mean, if row['status'] is equals to 1 it means checkbox should be checked on load? – aimme Sep 25 '15 at 21:01
  • @TaylorReed improved my answer, read EDITED part. i think that might help you :) – aimme Sep 25 '15 at 21:11