Jump to content

Deleting with checkboxes

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 post
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 post
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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...