Home > Article > Backend Development > How to Properly Delete Multiple Database Rows Using 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>
Alas, your code falters due to a few critical oversights:
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!