In websites or application systems, there are often situations where batch operations are required. For example, delete or modify a set of data. For a back-end developer, how to easily implement such batch operations? This article will introduce how to use PHP language to implement batch deletion of multiple selections.
1. Get the multi-select ID
First, we need to define the multi-select box in the front-end page to control which records are selected. In HTML, you can define a multi-select box like this:
<input type="checkbox" name="ids[]" value="1">
where name="ids[]"
means that this is an array type form control, and the selected value will be in the form of the array. method is submitted to the background. The value
attribute represents the value of this multi-select box.
We also need to define a submit button to submit the selected data. You can define a button like this:
<button type="submit" name="delete" value="1">删除选中</button>
where name="delete"
represents the name of the submitted operation, and value="1"
represents the value of the operation.
2. Receive the multi-select ID
In PHP, we can receive the submitted multi-select data through the $_POST
variable. You can use $_POST['ids']
to get the selected ID array.
We can use the following code to check whether there are selected records:
$ids = isset($_POST['ids']) && is_array($_POST['ids']) ? $_POST['ids'] : array(); if (empty($ids)) { die('请选中要删除的记录'); }
3. Delete the selected records
Next we will use the obtained ID array to Delete selected records. If you use the ORM framework, call the delete
method directly to complete the deletion operation. If you do not use an ORM framework, you need to write your own SQL statements to complete the deletion operation. The following is some sample code:
Use the ORM framework to delete records:
foreach ($ids as $id) { $model->delete($id); }
Use SQL statements to delete records:
$ids = implode(',', $ids); $sql = "DELETE FROM `table` WHERE `id` IN ($ids)";
The table
here means you want In the deleted data table, id
is the primary key field of the record you want to delete.
4. Complete batch deletion
Finally, we need to use batch deletion operations to delete multiple records at once. Using batch deletion operations can reduce the number of database connections, thereby improving the speed and efficiency of deletion.
The following code implements the batch deletion operation:
if (isset($_POST['delete'])) { $ids = isset($_POST['ids']) && is_array($_POST['ids']) ? $_POST['ids'] : array(); if (empty($ids)) { die('请选中要删除的记录'); } $ids = implode(',', $ids); // 使用ORM框架 foreach ($ids as $id) { $model->delete($id); } // 或者使用SQL语句 $sql = "DELETE FROM `table` WHERE `id` IN ($ids)"; $db->query($sql); header('Location: index.php'); }
In this code, we first obtain the selected ID data, and then convert it into a comma-separated string. Finally, we use this string to perform batch deletion operations. If the deletion is successful, it will jump back to the list page.
Summary
This article introduces how to use PHP language to implement batch deletion of multiple selections. First, we define the multi-select box and submit button in the front-end page to submit the selected data. Then, we receive and process the selected data in the background, and use the ORM framework or SQL statements to delete the selected records. Finally, we implemented batch deletion operations to improve deletion speed and efficiency.
The above is the detailed content of How to use PHP to delete multiple selections in batches. For more information, please follow other related articles on the PHP Chinese website!