Home  >  Article  >  Backend Development  >  How can I delete multiple rows from a MySQL database table using checkboxes in PHP, and what common errors should I avoid?

How can I delete multiple rows from a MySQL database table using checkboxes in PHP, and what common errors should I avoid?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-26 16:50:30616browse

How can I delete multiple rows from a MySQL database table using checkboxes in PHP, and what common errors should I avoid?

Multi-Row Deletion Using Checkboxes in PHP

The provided code aims to delete multiple rows from a MySQL database table. However, the current implementation is encountering an issue where no rows are being deleted. To address this problem, certain modifications to the code are required.

The first issue lies in how you receive the checkbox values. You must treat the checkbox values as an array, similar to:

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

This change will allow you to count and loop through the selected checkboxes for deletion.

Another error is the absence of the database connection in the query execution. You failed to pass $dbc to the mysqli_query() function. The correct syntax should be:

<code class="php">$result = mysqli_query($dbc, $sql);</code>

Here is the corrected code:

<code class="php"><?php
// Retrieve data from database and prepare HTML table
$dbc = mysqli_connect('localhost', 'root', 'admin', 'sample') or die('Error connecting to MySQL server');
$query = "select * from links ORDER BY link_id";
$result = mysqli_query($dbc, $query) or die('Error querying database');

// Start form and table for displaying links and checkboxes
echo '<form name="form1" method="post" action="">';
echo '<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">';

// Header row
echo '<tr>';
echo '<td colspan="4" align="center" bgcolor="#FFFFFF"><strong>Delete multiple links</strong></td>';
echo '</tr>';

echo '<tr>';
echo '<td align="center" bgcolor="#FFFFFF">#</td>';
echo '<td align="center" bgcolor="#FFFFFF"><strong>Link ID</strong></td>';
echo '<td align="center" bgcolor="#FFFFFF"><strong>Link Name</strong></td>';
echo '<td align="center" bgcolor="#FFFFFF"><strong>Link URL</strong></td>';
echo '</tr>';

// Data rows
while ($row = mysqli_fetch_array($result)) {
    echo '<tr>';
    echo '<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" value="' . $row['link_id'] . '"></td>';
    echo '<td bgcolor="#FFFFFF">' . $row['link_id'] . '</td>';
    echo '<td bgcolor="#FFFFFF">' . $row['link_name'] . '</td>';
    echo '<td bgcolor="#FFFFFF">' . $row['link_url'] . '</td>';
    echo '</tr>';
}

// Submit button
echo '<tr>';
echo '<td colspan="4" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" value="Delete"></td>';
echo '</tr>';

// Closing tags
echo '</table>';
echo '</form>';

// Delete rows if submit button is clicked
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);
    }

    // Redirect to a confirmation or success page after deletion
    if ($result) {
        echo '<meta http-equiv="refresh" content="0;URL=view_links.php">';
    }
}

mysqli_close($dbc);
?></code>

These modifications should address the issue and enable the code to delete multiple rows from your MySQL table using checkboxes.

The above is the detailed content of How can I delete multiple rows from a MySQL database table using checkboxes in PHP, and what common errors should I avoid?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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