Take the 2-minute tour ×

If it possible to edit my status field in my database when I check the checkbox?

What I want is, when I check multiple rows in my table it updates automatically the value of row['status'] in the database into yes after I submit.

Just like this,

Table APP

counter | status

100001  | 
100002  | 
100003  |   
100004  | 

And when I checked the 100001 and 100002 the value of status changed to yes.

counter | status

100001  | yes
100002  | yes
100003  |   
100004  | 

Below is my short code below.

$mysqli = new mysqli("localhost", "root", "", "app");
$result1 = $mysqli->query("SELECT * FROM APP");
echo'<table id="tfhover">
while($row = $result1->fetch_assoc()){
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="'.$row['counter'].'"></td>
<input id='edit' type='submit' class='button' name='edit' value='Edit'/> 



If it is possible to do this? Help ?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

may be you can try this code at php

         $status = "yes";
         foreach($_POST['checkbox'] as $key=>$value){
             $stmt = $mysqli->prepare("UPDATE APP SET status=? WHERE counter= ?");
             $stmt->bind_param('ss', $status, $value);
         echo "No changes";
share|improve this answer
Why not use foreach? This also doesn't validate the user input, so it could be susceptible to SQL injections. –  Devon Nov 13 '13 at 3:34
because i prefer using for than foreach... i seldom use foreach.. –  Hans Nov 13 '13 at 3:39
@Hans this is GREAT it works :D –  altaire lastname Nov 13 '13 at 3:42
:), glad, if this code can help you –  Hans Nov 13 '13 at 3:44
No problem. Glad to help. I believe when the $_POST["checkbox"] is viewed (you can test this with print_r($_POST['checkbox']);) you will see that it will be: array( [0] => 100001, [1] = 100002 ) and so on. So the $key would be 0, the $value would be 100001. The value is what you want. If you just had a single level array where the keys were [100001], [100002] then you wouldn't need the key=> –  Devon Nov 13 '13 at 4:54
show 8 more comments

The name="checkbox[]" will create an array in $_POST["checkbox"] for you to use. The array will contain the values of all the checkboxes you selected.

share|improve this answer
and How can I do that? –  altaire lastname Nov 13 '13 at 3:15
Well I won't write the code for you. But the easiest way to analyze all the values of an array is using us3.php.net/manual/en/control-structures.foreach.php –  Devon Nov 13 '13 at 3:16
add comment

Your Answer


