Jump to content

How can I get a checkbox to delete a item


logixxxx

Recommended Posts

Hello, how can I use a checkbox to delete multiple items with how I did this php?

 

<form action=""><?php
db();
$result = mysql_query("SELECT timestamp, id, title FROM " . $mysql["db_prefix"] . "main ORDER BY id DESC");

while($row = mysql_fetch_array($result)) {
    $date  = date("l F d Y",$row['timestamp']);
    $id = $row['id'];
    $title = strip_tags(stripslashes($row['title']));

    if (strlen($title) >= 50) {
        $title = substr($title, 0, 50);
        $title = $title . "...";
    }
    echo "	
<input type=\"checkbox\" name=\"check\" value=\"\"> <b>$date</b> - <a href=\"editform.php?id=" . $id . "\">"  . $title . "</a><br />";
}

mysql_close();

?>
</form>

Give chkboxes the same name ending with "[]"  eg del_id[] so they get sent as an array.

 

Give each chkbox the value of the id to be deleted.

 

<?php
if (isset($_GET['del_id'])) {
    $ids = join ("','", $_GET['del_id']);
    $sql = "DELETE FROM mytable WHERE id IN ('$ids')";
    echo $sql;
}
?>
<hr />
<form>
<input type='checkbox' name='del_id[]' value='1'> item 1 <br />
<input type='checkbox' name='del_id[]' value='2'> item 2 <br />
<input type='checkbox' name='del_id[]' value='3'> item 3 <br />
<input type='submit' name='action' value='Delete selected'>
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.