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']; ?>" />
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);
This ensures that the query is executed using the established database connection.
Here is the updated PHP code:
... while ($row = mysqli_fetch_array($result)) { ...... // 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); } ... <input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>" /> ...
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!