Determine whether to delete a single selection or multiple selections
1. A single line is written to the delete.php file through get parameters. The corresponding ID.
2. For multiple deletions, the corresponding ID is passed to the delete.php page through POST.
3. If neither of these two is met, then we can regard the data as illegal.
if (is_array($_POST['id'])) { $id = join(',', $_POST['id']); } elseif (is_numeric($_GET['id'])) { $id = (int) $_GET['id']; } else { echo '数据不合法'; exit; }
Combined SQL statements
We have previously explained to you in the MySQL chapter that you can use the in sub-statement when deleting.
Similarly here, we can use the in sub-statement to achieve the effect.
The join function changes the id passed by multi-select deletion into the format of 3,4,5. The final effect of executing the SQL statement of multi-select deletion is:
delete from user where id in(3,4,5,6,8);
The effect of the single-select delete statement is:
delete from user where id in(3)
This way We have achieved single-selection and multi-selection adaptive effects.
$sql = "delete from user where id in($id)";
The final complete code demonstration is as follows:
<?php include 'connection.php'; if (is_array($_POST['id'])) { $id = join(',', $_POST['id']); } elseif (is_numeric($_GET['id'])) { $id = (int) $_GET['id']; } else { echo '数据不合法'; exit; } $sql = "delete from user where id in($id)"; $result = mysqli_query($conn, $sql); if ($result) { echo '删除成功'; } else { echo '删除失败'; }