Home > Database > Mysql Tutorial > How to Delete Multiple MySQL Rows Using Checkboxes in PHP?

How to Delete Multiple MySQL Rows Using Checkboxes in PHP?

Susan Sarandon
Release: 2024-11-26 18:48:11
Original
938 people have browsed it

How to Delete Multiple MySQL Rows Using Checkboxes in PHP?

Deleting Multiple Rows Using Checkboxes in PHP

A user has encountered an issue when attempting to delete multiple rows from a MySQL database table using checkboxes. The provided PHP code is not executing the deletion process, despite the data being populated in the table.

To resolve this issue, the PHP code should be modified as follows:

<input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>" />
Copy after login

By using the [] syntax in the name attribute, the checkbox values will be treated as an array, allowing us to iterate over them in the deletion process.

Additionally, the database connection should be passed to the query as follows:

$result = mysqli_query($dbc, $sql);
Copy after login

This ensures that the query is executed using the established database connection.

Here is the updated PHP code:

...
while ($row = mysqli_fetch_array($result)) {
...

  
    <input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>" />
  
...
 
...

// Check if delete button active, start this
if (isset($_POST['delete'])) {
  $checkbox = $_POST['checkbox'];
  for ($i = 0; $i < count($checkbox); $i++) {
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM links WHERE link_id='$del_id'";
    $result = mysqli_query($dbc, $sql);
  }
...
Copy after login

With these modifications, the checkbox variable will be an array of selected link IDs, which can then be iterated over to execute the deletion queries.

The above is the detailed content of How to Delete Multiple MySQL Rows Using Checkboxes in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template