0

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

3
  • What's the reason to run a query for each record you want to delete? Commented Aug 13, 2011 at 10:49
  • He probably doesn't know about the IN. :-) Commented Aug 13, 2011 at 10:54
  • @ninetwozero: I'm afraid you're right. ;) Commented Aug 13, 2011 at 11:01

4 Answers 4

1

your mixing up GET with POST, change form method to post

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.