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

时间:2011-06-17 16:11:51

标签: php mysql dynamic html-table

这是我的代码 -

<?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>

我想要的是表的数据行,其复选框被选中, 单击“删除”按钮从数据库中删除。

我尝试了这些但是效果不好。

请就上述代码如何删除所选行建议我 从DB中,单击“删除”按钮。

2 个答案:

答案 0 :(得分:3)

理论上它应该可以工作,但我还没有测试过

<?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 :(得分:2)

经过测试的版本:

将第45-55行更改为

if($_POST['delete']){

  $i = 0;
  while(list($key, $val) = each($_POST['checkbox'])) {
    $sql = "DELETE FROM $tbl_name WHERE id='$val'";
    mysql_query($sql);
    $i += mysql_affected_rows();
  }

  // if successful redirect to delete_multiple.php
  if($i > 0){
    echo '<meta http-equiv="refresh" content="0;URL=delete_multiple.php">';
  }
}

该文件应以“&lt;?php”开头,并且混合使用“&lt;?”会被认为是错误的风格和“&lt;?php”在同一个文件中(第31-35行和第43行)。

编辑:忘记第45行的$ delete中的$ _POST ['']。如果你有register_globals ini-directive,它没关系(虽然它仍然是危险的和糟糕的风格)。