简体   繁体   中英

Delete multiple rows mysql with checkbox

please help in deleting multiple rows from a table.

home.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

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

your mixing up GET with POST, change form method to 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

$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. Change method="get" to method="post" on home.php

I would change it to the following...

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)!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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