PHP database op...LOGIN

PHP database operation: batch and specific deletion of users

We are in section 13.3 "Making a list display through steps". Before deletion, there are single row deletion of data and deletion of multiple rows of data.

Determine whether to delete a single selection or multiple selections

1. A single line is passed to the delete.php file through get parameters. Write the corresponding ID in .

2. Multiple deletions pass the corresponding ID to the delete.php page through POST.

3. If neither of these two is consistent, 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 the multi-select deletion into the format of 3, 4, 5. The final effect of the SQL statement for multi-select deletion is:

delete from user where id in(3,4,5,6,8);

And single-select deletion The effect of the statement is:

delete from user where id in(3)

In this way, we achieve the 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 '删除失败';
}
?>


Next Section
<?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 '删除失败'; } ?>
submitReset Code
ChapterCourseware