更新时间:2023-12-06 16:01:52
将整个表格和按钮包裹在一个表单中:
Wrap the whole table and button in a form:
<form action="" method="post">
...
...
</form>
在你的 while 循环中,在删除单元格中添加一个复选框:
Inside your while loop, add a checkbox to the delete cell:
// concatenate or echo as required
<input type="checkbox" name="delete[]" value="<?php echo (int)$rows['id']; ?>" />
确保您的按钮属于 submit
类型:
Make sure your button is of type submit
:
<input type="submit" value="Delete Selected" />
在 PHP 方面:
if(isset($_POST['delete']) && is_array($_POST['delete']) && count($_POST['delete']) > 0){
foreach($_POST['delete'] as $deleteId){
// run query to delete $deleteId
}
}
这是可行的,因为 PHP 将名称为 delete[]
的复选框解析为原生数组
This works because PHP parses the checkboxs with the names delete[]
into a native array