PDA

View Full Version : Delete Multiple URLs from admin page


millsi80
12-09-2008, 02:24 AM
Hi,

I have a page where I can delete urls that are submitted by users that don't work anymore. The problem is that I can only delete 1 url at a time. I was wondering if there was a way to modify (or use) the following to be able to delete all the urls that are submitted.

// Delete a URL
if($_GET['action']){
$id=$_GET['id'];
$query="DELETE FROM hsrs WHERE id=$id";
$result = mysql_query($query) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());
header ("location: $url");
exit;
}

Fumigator
12-09-2008, 04:37 PM
You can do


DELETE FROM table1
WHERE id IN (1, 2, 3, 4, 5)


So to apply this, you'll probably want to build the query string's "IN" clause with a loop, evaluating your form's checkboxes (or whatever) inside the loop.

millsi80
12-09-2008, 11:36 PM
Thanks for the reply!

I dont quite understand what you mean by "build the query string's "IN" clause with a loop, evaluating your form's checkboxes (or whatever) inside the loop. "

Fumigator
12-10-2008, 08:49 PM
Yeah it's hard to explain... maybe showing it will help. This is PHP code, so if you use a different server language you'll have to adapt it.

You start with the basic query...

$query = "DELETE FROM table1 WHERE id IN (";


Then you build the values for the IN clause using a loop...

//this assumes your checkboxes are in an array called chkBoxes and the checkbox values are the IDs
if (is_array($_POST['chkBoxes'])) {
foreach ($_POST['chkBoxes'] as $chkBoxID) {
//add an ID to the query
$query . = "$chkBoxID,";
}

//remove the last comma and close the IN clause with a right paren
$query = substr($query, 0, -1);
$query . = ")";

$result = mysql_query($query);
if (!$result) {
die ("Fatal SQL Error for query $query<br>Error: ".mysql_error());
}
} else {
echo "Nothing to delete."
}

oesxyl
12-10-2008, 09:18 PM
Yeah it's hard to explain... maybe showing it will help. This is PHP code, so if you use a different server language you'll have to adapt it.

You start with the basic query...

$query = "DELETE FROM table1 WHERE id IN (";


Then you build the values for the IN clause using a loop...

//this assumes your checkboxes are in an array called chkBoxes and the checkbox values are the IDs
if (is_array($_POST['chkBoxes'])) {
foreach ($_POST['chkBoxes'] as $chkBoxID) {
//add an ID to the query
$query . = "$chkBoxID,";
}

//remove the last comma and close the IN clause with a right paren
$query = substr($query, 0, -1);
$query . = ")";

$result = mysql_query($query);
if (!$result) {
die ("Fatal SQL Error for query $query<br>Error: ".mysql_error());
}
} else {
echo "Nothing to delete."
}


you could replace this lines:

foreach ($_POST['chkBoxes'] as $chkBoxID) {
//add an ID to the query
$query . = "$chkBoxID,";
}

//remove the last comma and close the IN clause with a right paren
$query = substr($query, 0, -1);


with this:

$query .= join(',',$_POST['chkBoxes']);


best regards

Fumigator
12-10-2008, 10:14 PM
Yeah I always forget about implode() (or join(), same thing). Thanks Oesxyl.