douhui9192 2011-06-17 16:11
浏览 24
已采纳

使用复选框表格DB表删除多行

Here is my code -

<?php
$host = "localhost"; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name = "test"; // Database name
$tbl_name = "test_mysql"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
$sql = "SELECT * FROM $tbl_name";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
        <td>
            <form name="form1" method="post" action="">
                <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
                    <tr>
                        <td bgcolor="#FFFFFF">&nbsp;</td>
                        <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong></td>
                    </tr>
                    <tr>
                        <td align="center" bgcolor="#FFFFFF">#</td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
                    </tr>
                    <?php
                    while ($rows = mysql_fetch_array($result)) {
                        ?>
                        <tr>
                            <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox"
                                                                        id="checkbox[]" value="<? echo $rows['id']; ?>">
                            </td>
                            <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
                            <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
                            <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
                            <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
                        </tr>
                        <?php
                    }
                    ?>
                    <tr>
                        <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete"
                                                                                value="Delete"></td>
                    </tr>
                    <?
                    // Check if delete button active, start this
                    if ($delete) {
                        for ($i = 0; $i < $count; $i++) {
                            $del_id = $checkbox[$i];
                            $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
                            $result = mysql_query($sql);
                        }
                        // if successful redirect to delete_multiple.php
                        if ($result) {
                            echo "<meta http-equiv="refresh" content="0;URL = delete_multiple . php">";
    }
                    }
                    mysql_close();
                    ?>
                </table>
            </form>
        </td>
    </tr>
</table>

What I want is that data rows of the table, whose checkboxes are checked, to be Deleted from the Database on click of Delete button.

I have tried these but it doesn't work well.

Please suggest me regarding the above code how to delete the selected rows from DB, onclick of Delete button.

  • 写回答

2条回答 默认 最新

  • doolo00026 2011-06-17 16:39
    关注

    Theoretically it should work, but I have not tested

    <?php
        $host = 'localhost'; // Host name
        $username = ''; // Mysql username
        $password = ''; // Mysql password
        $db_name = 'test'; // Database name
        $tbl_name = 'test_mysql'; // Table name
    
        // Connect to server and select databse.
        mysql_connect($host, $username, $password) or die('cannot connect');
        mysql_select_db($db_name) or die('cannot select DB');
    
        $sql = 'SELECT * FROM `'.$tbl_name.'`';
        $result = mysql_query($sql);
    ?>
    
    <table width="400" border="0" cellspacing="1" cellpadding="0">
        <tr>
            <td>
                <form name="form1" method="post" action="">
                <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
                <tr>
                    <td bgcolor="#FFFFFF">&nbsp;</td>
                    <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
                </tr>
                <tr>
                    <td align="center" bgcolor="#FFFFFF">#</td>
                    <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
                    <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
                    <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
                    <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
                </tr>
                <?php while ($rows = mysql_fetch_array($result)): ?>
                <tr>
                    <td align="center" bgcolor="#FFFFFF"><input name="need_delete[<? echo $rows['id']; ?>]" type="checkbox" id="checkbox[<? echo $rows['id']; ?>]" value="<? echo $rows['id']; ?>"></td>
                    <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
                    <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['name']); ?></td>
                    <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['lastname']); ?></td>
                    <td bgcolor="#FFFFFF"><? echo htmlspecialchars($rows['email']); ?></td>
                </tr>
                <?php endwhile; ?>
                <tr>
                    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
                </tr>
                <?php
                    // Check if delete button active, start this
                    if ( ! empty($_POST['delete'])) {
                        foreach ($_POST['need_delete'] as $id => $value) {
                            $sql = 'DELETE FROM `'.$tbl_name.'` WHERE `id`='.(int)$id;
                            mysql_query($sql);
                        }
                        header('Location: delete_multiple.php'); exit();
                    }
                    mysql_close();
                ?>
                </table>
                </form>
            </td>
        </tr>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端