Jump to content

[SOLVED] Mass Delete, Checkboxes


ari_aaron

Recommended Posts

Is there a good code to delete multiple SQL entries with checkboxes? I have a form with a bunch of checkboxes with value="2512", or whatever which goes to the ID column in my SQL table. I want it so that whatever boxes that are checked are selected, so 'SELECT * FROM table WHERE ID="2323" OR ID="1234"'

 

 

I probably can explain better if someone dosn't get what i'm asking.

Link to comment
https://forums.phpfreaks.com/topic/44384-solved-mass-delete-checkboxes/
Share on other sites

//html form with a bit of javascript to make it easier

//html code or form
<head>
<script type="text/javascript">
<!--
function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
if(!document.forms[FormName])
	return;
var objCheckBoxes = document.forms[FormName].elements[FieldName];
if(!objCheckBoxes)
	return;
var countCheckBoxes = objCheckBoxes.length;
if(!countCheckBoxes)
	objCheckBoxes.checked = CheckValue;
else
	// set the check value for all check boxes
	for(var i = 0; i < countCheckBoxes; i++)
		objCheckBoxes[i].checked = CheckValue;
}
// -->
</script>
</head>
<input type="button" onclick="SetAllCheckBoxes('myForm', 'deletelist[]', true);" value="Select All">
<input type="button" onclick="SetAllCheckBoxes('myForm', 'deletelist[]', false);" value="DeSelect All">
<form action='deletepms.php' method='post' name='myForm'>
<INPUT TYPE='CHECKBOX' value='1' name='deletelist[]' multiple='multiple'>1
<INPUT TYPE='CHECKBOX' value='2' name='deletelist[]' multiple='multiple'>2
<INPUT TYPE='CHECKBOX' value='3' name='deletelist[]' multiple='multiple'>3
<INPUT TYPE='CHECKBOX' value='4' name='deletelist[]' multiple='multiple'>4
<INPUT TYPE='CHECKBOX' value='5' name='deletelist[]' multiple='multiple'>5
<INPUT TYPE='CHECKBOX' value='6' name='deletelist[]' multiple='multiple'>6
<input type='submit' value='Delete Selected'>
</form>

the php code that proccesses the info submit

$del = $_POST['deletelist'];
if($del == "") { echo "you did not select any pms"; exit; }
$del2 = implode(", ", $del); //lists the values in an array seperated by commas
$query = "delete FROM pm WHERE username = 'user' && id IN ($del2)";
$result = mysql_query($query) or die(mysql_error());
echo"All selected messages have been deleted.";

either that or you can run like a foreach function

$del = $_POST['deletelist'];
foreach($del as $delete)
{
$query = "delete FROM pm WHERE username = 'user' && id = '$delete'";
$result = mysql_query($query) or die(mysql_error());
echo "PM #".$delete." had been deleted.";
}

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.