If it possible to changed multiple rows value usin

2019-08-21 11:12发布

问题:

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.

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

<?
if(isset($_POST['edit'])) 
{

}
?>

If it is possible to do this? Help ?

回答1:

may be you can try this code at php

<?
if(isset($_POST['edit'])) 
{
     if(is_array($_POST['checkbox'])){
         $status = "yes";
         foreach($_POST['checkbox'] as $key=>$value){
             $stmt = $mysqli->prepare("UPDATE APP SET status=? WHERE counter= ?");
             $stmt->bind_param('ss', $status, $value);
             $stmt->execute();             
         }
     }else{
         echo "No changes";
     }
}
?>


回答2:

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.