javascript - 是否可以使用复选框更改多行值?

标签 javascript php mysql mysqli

当我选中复选框时是否可以编辑数据库中的状态字段?

我想要的是,当我检查表中的多行时,它会在我提交后自动将数据库中的 row['status'] 的值更新为 yes。

就这样,

表格应用

counter | status

100001  | 
100002  | 
100003  |   
100004  | 

当我检查 100001 和 100002 时,status 的值更改为 yes。

counter | status

100001  | yes
100002  | yes
100003  |   
100004  | 

下面是我的简短代码。

<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'])) 
{

}
?>

是否可以这样做?帮忙?

最佳答案

也许你可以尝试这个代码 在 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";
     }
}
?>

关于javascript - 是否可以使用复选框更改多行值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19944345/

相关文章:

javascript - 为什么 Exports 和 require() 创建全局变量?

javascript - 定义整数类型的数据,但使用 node.js 模型返回字符串类型的数据

javascript - php 是否有内置的数组函数可以对 bool 结果进行排序/赋值?

php - 加速实时搜索 js, php, mysql

mysql - 使用 where 和语句在 1 列中搜索

MySQL 5.7 中忽略 INNODB 表计数查询的索引

javascript - 我无法访问 <asp :textbox> value in code behind file

php - 像谷歌图像搜索那样对齐图像

php - 查询唯一列数据

javascript - Div 页面过渡