Jump to content

[SOLVED] mysql read from checkboxes.. foreach?


Dragen

Recommended Posts

Okay, I've got this code to create a checkbox...

<?php
echo "<input type=\"checkbox\" name=\"id\" value=\"" . $row['id'] . "\" />"
?>

On the form I usually end up with at least two or three checkboxes using a a while statement to read from a database. The only difference is the value, which are all numerical.

When the form gets processed it goes through this code:

$sql = "DELETE FROM " . $type . " WHERE id = '" . $_POST['id'] . "'";

Which gets the id from the checkbox and deletes that row in the table.

The problem is if I select more than one checkbox it only deletes the last one and not the others... I presume I'd need a foreach statement to go through all the checked ones, but I'm not sure how to do so.

If anyone can help?

Thanks

You need

<?php
echo "<input type=\"checkbox\" name=\"id[]\" value=\"" . $row['id'] . "\" />"
?>

 

NOTE: name=id[]

 

To process

$idlist = join (',' , $_POST['id']);    // eg gives "1,3,7"

mysql_query ("DELETE FROM table WHERE id IN ($idlist)");

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.