Page 1 of 1

deleting bulk

Posted: Mon Oct 31, 2005 10:07 am
by elecktricity
just a quick question, on one page I would have a bunch of checkboxes like so:

Code: Select all

<input type="checkbox" name="val" value="1"><br>
<input type="checkbox" name="val" value="2"><br>
<input type="checkbox" name="val" value="3"><br>
<input type="checkbox" name="val" value="4"><br>
<input type="checkbox" name="val" value="5"><br>
//continues
and I would like to select a few of them and click a delete button which would go to a place like this:

Code: Select all

<?PHP
$val = $_POST['val'];
mysql_query("DELETE FROM pm WHERE val=$val");
?>
which would work fine for deleteing one row but I would like it to delete a few more than that and wouldnt really want to make a huge page with a lot of 'if' statements or anything, im thinking a loop might work or something like that. What would you guys suggest on this one?

Posted: Mon Oct 31, 2005 10:10 am
by shiznatix
make your html name="val[]"

then do

Code: Select all

foreach ($_POST['val'] as $value)
{
  $sql = '
    DELETE FROM table_name WHERE value = "'.$value.'"
  ';

  $do_query = mysql_query($sql) or die(mysql_error());
}
use that idea and put it into your current script

EDIT: or you can do somthing like

Code: Select all

$sql = 'DELETE FROM table name WHERE ';

foreach ($_POST['val'] as $value)
{
  $sql .= 'row_name ='.$value.', ';
}

//then take the last 5 characters off of $sql which is the space, comma then the word AND

//then do the query, this would make only one mysql query and make for a faster script

Posted: Mon Oct 31, 2005 10:23 am
by elecktricity
sure was a quick reply thanks, ill try it out

Posted: Mon Oct 31, 2005 10:40 am
by feyd
following shiznatix's logic path of naming, you can do the following as well:

Code: Select all

$vals = implode('\',\'',array_map('intval',$_POST['val']));
$sql = "DELETE FROM `table_name` WHERE `value` IN('{$vals}')";
$do_query = mysql_query($sql) or die(mysql_error());

Posted: Mon Oct 31, 2005 11:15 am
by elecktricity
well it works great thanks for the help