I have a form that has images that are displayed from a database. Beside each image, I have a checkbox.
How do I set things up so that when i check multiple checkboxes and click Submit, the corresponding image/record will be deleted from the database? Please help....
Deleting from database by checking multiple checkboxes
Moderator: General Moderators
It' s like this:
You can use case.. switch if there are more than 3 checkboxes or repetive structure but the logic is the same.
Code: Select all
if (isset($_POST["Submit"]))
{
if (isset($_POST["FirstCheckbox"]) ){
//execute the mysql code for deletion of image : DELETE TableName.NameOfColumn FROM TableNAme WHERE NameOfColumn='NameOfImage'
}
if (isset($_POST["SecondCheckbox"])){// exactly like the one before}
}My checkboxes are going to be dynamically generated so the name is going to be:
Code: Select all
<input type='checkbox' name='deleteimg[]' value='1'>-
Rhys.mataira
- Forum Newbie
- Posts: 7
- Joined: Mon Oct 22, 2007 2:18 pm
Yes. Somebody suggested using the implode statement and that worked like a charm.
Thanks for all of your help...
Code: Select all
$ids = implode(', ', $_POST['chk']);
$sql = "DELETE FROM table_name WHERE ID IN ($ids)";-
Rhys.mataira
- Forum Newbie
- Posts: 7
- Joined: Mon Oct 22, 2007 2:18 pm
well u could do this
Now in the 15 minutes ive browsed this site ive found a much better solution which i think might have been more on track with your original ideas
and all you do for your html side of things is name your deleteimg inputs deleteimg[]
Code: Select all
$sql="DELETE FROM 'yourtable' WHERE ";
$findme='eleteimg';
$counter=0;
foreach($_POST as $mystring => $value){
$strSearch = strpos($mystring, $findme);
if(!empty($strSearch)) {
$sql .= (!$counter) ? "'id' =".$value : " , 'id' =".$value ;
$counter++;
}
}Code: Select all
$sql="DELETE FROM 'yourtable' WHERE ";
$counter=0;
foreach($_POST['deleteimg'] as $value){
$sql .= (!$counter) ? "'id' =".$value : " , 'id' =".$value ;
$counter++;
}