简体   繁体   English

用复选框删除多行 mysql

[英]Delete multiple rows mysql with checkbox

please help in deleting multiple rows from a table.请帮助从表中删除多行。

home.php主页.php

 <form action="del.php" method="get" enctype="multipart/form-data">
<?php
while($row=mysql_fetch_array($rs))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<?php echo $row['id']; ?>]" type="checkbox" id="checkbox[<?php echo $row['id']; ?>]" value="<?php echo $row['id']; ?>"></td>
<td><?php echo $row['listingtype'];?></td>
<td><?php echo $row['propertyname'];?></td>
<td><?php echo $row['price'];?></td>
<td><?php echo $row['listdate'];?></td>


</tr>
<?php
}
?>

</table>
<tr>
 <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr></form>

del.php del.php

<?php
                 include "include/db.php";
                 $tbl_name='propertylistingbasicinfo';

                // 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: home.php');
                }

            ?>

Error: When i click on delete button, blank screen come.错误:当我单击删除按钮时,出现空白屏幕。 And url looks like this:http://localhost/realestate/del.php?need_delete%5B2%5D=2&delete=Delete url 看起来像这样:http://localhost/realestate/del.php?need_delete%5B2%5D=2&delete

your mixing up GET with POST, change form method to post您将 GET 与 POST 混淆,将表单方法更改为发布

You need something similar to this in your form您的表单中需要与此类似的内容

<input type="checkbox" name="record[]" value="<?php echo $id ?>">

then you can use implode() function to concatenate ids and remove them然后您可以使用 implode() function 连接 id 并删除它们

$deleted = implode(',',$_POST['record']);
$query = mysql_query("delete from table where id in ('$deleted') ");

Not tested but this the idea.未经测试,但这是这个想法。

The problem is that you are submitting the form with method="get" and in del.php, you are accessing values from $_POST.问题是您使用 method="get" 提交表单,并且在 del.php 中,您正在从 $_POST 访问值。 Change method="get" to method="post" on home.php在 home.php 上将 method="get" 更改为 method="post"

I would change it to the following...我将其更改为以下...

home.php : home.php

<form action="del.php" method="post" enctype="multipart/form-data">
    <table>
    <?php while($row=mysql_fetch_array($rs)) { ?>
        <tr>
            <td align="center" bgcolor="#FFFFFF">
                <input name="need_delete[]" value="<?php echo $row['id']; ?>" type="checkbox" id="checkbox[<?php echo $row['id']; ?>]" />
            </td>
            <td><?php echo $row['listingtype'];?></td>
            <td><?php echo $row['propertyname'];?></td>
            <td><?php echo $row['price'];?></td>
            <td><?php echo $row['listdate'];?></td>
        </tr>
    <?php } ?>
    <tr>
         <td colspan="5" align="center" bgcolor="#FFFFFF">
            <input name="delete" type="submit" id="delete" value="Delete" />
        </td>
    </tr>
</table>

del.php:

<?php
    //Get the db-file and set the table name
    include "include/db.php";
    $tbl_name='propertylistingbasicinfo';

    // Check if delete button active, start this
    if ( !empty($_POST['delete'])) {

        //Let's sanitize it
        $sanitized_post_ids = array();
        foreach($_POST['need_delete'] as $id ) $sanitized_post_ids[] = intval($id);

        //Get the ids and add a trailing ",", but remove the last one
        $in_str = substr(implode(',', $sanitized_post_ids), 0, -1);

        //Build the sql and execute it
        $sql = 'DELETE FROM ' . $tbl_name ' WHERE id IN (' . $in_str . ')'; 
        mysql_query($sql);

        //Redirect!
        header('Location: home.php');
    }
?>

Hope this works out for you (and that I didn't manage to add a typo or similar misshaps)!希望这对您有用(并且我没有设法添加错字或类似的失误)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM