面对从 MySQL 堡垒中清除多行的任务,您在编码智慧的深处寻求启蒙。在着手进行数据清理工作时,您似乎遇到了难以克服的障碍。
您编写的代码虽然意图勇敢,但未能执行其行消灭的预期目的。决心克服这个障碍,您通过向受人尊敬的编码社区展示您的代码来寻求指导:
<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>
唉,您的代码由于以下原因而不稳定一些关键的疏忽:
通过这些纠正,您的代码将像数字魔法师一样消除行。带着新的信心开始您的数据清理任务!
以上是如何在 PHP 中使用复选框正确删除多个数据库行?的详细内容。更多信息请关注PHP中文网其他相关文章!