当我选中复选框时是否可以编辑数据库中的状态字段?
我想要的是,当我检查表中的多行时,它会在我提交后自动将数据库中的 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/