Home  >  Article  >  Backend Development  >  How to Properly Delete Multiple Database Rows Using Checkboxes in PHP?

How to Properly Delete Multiple Database Rows Using Checkboxes in PHP?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-25 19:42:02134browse

How to Properly Delete Multiple Database Rows Using Checkboxes in PHP?

Collaboratively Deleting Database Rows with Checkboxes in PHP

Faced with the task of purging multiple rows from your MySQL stronghold, you've sought enlightenment in the depths of coding wisdom. While embarking on this data-cleansing endeavor, you've seemingly run into an insurmountable roadblock.

The code you've crafted, though valiant in intent, fails to execute its intended purpose of row annihilation. Determined to overcome this obstacle, you seek guidance by presenting your code to the esteemed coding community:

<code class="html">// PHP witchcraft to connect to MySQL
$dbc = mysqli_connect('localhost', 'root', 'admin', 'sample') or die('MySQL is out to get you!');
$query = "select * from links ORDER BY link_id";
$result = mysqli_query($dbc, $query) or die('Whoops! Query went awry!');
$count = mysqli_num_rows($result);</code>
<code class="html">// Assemble your army of checkboxes
while ($row = mysqli_fetch_array($result)) {
    echo <<<HTML
<tr>
    <td><input type="checkbox" name="checkbox[]" value="$row[link_id]"></td>
    <td>$row[link_id]</td>
    <td>$row[link_name]</td>
    <td>$row[link_url]</td>
</tr>
HTML;
}</code>
<code class="html">// The arsenal of deletion awaits your command
if (isset($_POST['delete'])) {
    // Retrieve your checked victims
    $checkbox = $_POST['checkbox'];

    // Summon the wrath of deletion upon each victim
    foreach ($checkbox as $del_id) {
        $sql = "DELETE FROM links WHERE link_id='$del_id'";
        // Unleash the purging power!
        $result = mysqli_query($dbc, $sql);
    }

    // If successful, redirect to the crime scene
    if ($result) {
        echo '<meta http-equiv="refresh" content="0;URL=view_links.php">';
    }
}</code>

The Sage's Solution

Alas, your code falters due to a few critical oversights:

  1. Array or Single: Your checkbox input tags lack the [] suffix to treat them as an array. Without it, PHP won't recognize multiple selections correctly. Use name="checkbox[]".
  2. Database Summoning: Your query execution line was missing the connection variable. It should be $result = mysqli_query($dbc, $sql);.

With those rectifications, your code shall banish rows like a digital sorcerer. Embark on your data-cleansing quest with renewed confidence!

The above is the detailed content of How to Properly Delete Multiple Database Rows Using Checkboxes in PHP?. 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