Jump to content

Deleting with checkboxes


vchris

Recommended Posts

I am working on a project and each record has it's own checkbox. These checkbox are to delete the selected record. You can have 1 record like you can have all records (could be hundreds). I'm not sure how to get every value of every checkbox and I'm not sure of the delete mysql query for those records.I tried this:

<input type=\"checkbox\" name=\"itemdel\" value=\"" . $row['linkid'] . "\">

Which only display 1 number.I also tried this:

<input type=\"checkbox\" name=\"itemdel[]\" value=\"" . $row['linkid'] . "\">

Which only display the word "Array".My display statement:

echo $_POST['itemdel'];

I basically only need a list of selected values and need someway to work with it.Thanks

Link to comment
Share on other sites

With name="itemdel" you will get the value of the last checked box.With name="itemdel[]" you get an array with all the values of the checked boxes. Just var_dump($_POST['itemdel']); and you'll see them.

Link to comment
Share on other sites

This is what is displayed:

array(2) { [0]=>  string(1) "7" [1]=>  string(1) "5" }

Could I do this?

$query = "DELETE FROM mf_links WHERE linkid IN (" . var_dump($_POST['itemdel[]']) . ")";

Or would I have to create a loop?

Link to comment
Share on other sites

You would need to use a foreach, and if you dont know how to check if a checkbox is checked here is how:

if($_POST[$foreachnum] == true){//delete from table}

:) Im sure you already knew that though :)

Link to comment
Share on other sites

You can use foreach to make the string of those numbers:

$str = "";foreach($_POST['idemid'] as $value) {$str .= (int)$value.",";}$str = substr($str,0,-1);$query = "DELETE FROM mf_links WHERE itemid(".$str.")";...

Link to comment
Share on other sites

hmmm interesting... but this will only delete 1 record won't it? Is it possible to have those values separated with commas so I can do WHERE linkid IN (1,2,3)?

Link to comment
Share on other sites

Right, implode takes an array of elements, and returns a string that is the list of elements joined by whatever string you give it (in this case, a comma). If there is only one element, it will just return that element.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...