Forum Moderators: coopster

Message Too Old, No Replies

Deleting Multiple Items Using Checkboxes!

how?

         

dreamcatcher

1:08 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi everyone,

This is my first post on this forum. :)

I`ve been learning PHP for a couple of months now and I`ve run into this problem.

I have a list of e-mail addresses that each have a checkbox beside it. I want to be able to delete the address for any box that is checked. Now it works for one box, but if I check say two checkboxes, the last e-mail address that was added to the database gets deleted, but not the other one.

I am using:

<input type="checkbox" name="checkmail[]" value="$checkMail">

to delete the entries I`m using the following:

if ($checkmail)

{

$querydeleteemail = "DELETE FROM list WHERE post_email = '$checkmail'";
$resultdelete = mysql_query($querydeleteemail);
emailDelete();
exit;

}

I assume I need a foreach loop? Can anyone tell me exactly how to write a for each loop, because I seem to be having some problems.

Thank you for the help!

Birdman

1:23 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello and welcome to Webmaster World!

This should do you:

for ( $i = 0, $i < count( [php.net]$checkmail), $i++ ){
$query = "DELETE FROM list WHERE post_email = '$checkmail[i]'";
mysql_query( [php.net]$query);
}

dreamcatcher

1:52 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Birdman, thanks for the help and introduction!

I placed what you said in the code, but I`m getting a parse error message telling me its expecting a ";" after the for ( $i = 0, $i < count($checkmail), $i++ ) line.

:(

dreamcatcher

1:59 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Its ok, I figured it out. It should have been:

for ($i = 0; $i < count($checkmail); $i++)

Thanks very much for the help.

:)

andreasfriedrich

2:06 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




mysql_query [php.net](
sprintf [php.net](
"DELETE [mysql.com] FROM table
WHERE field IN [mysql.com] (%s)",
implode [php.net](',', $checkmail)));

will do the same more efficiently.

Andreas

Birdman

2:20 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Andreas, I knew there was a better way. So that example only queries the db once?

dreamcatcher

2:27 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Andreas. I figured there was a database query that would do the trick, but didn`t know what. I`m new to this stuff, so I`m not that familiar with it at the moment.

Anyway, I went with Birdmans syntax and it works like a charm. Just one thing, in the $query = "DELETE FROM list WHERE post_email = '$checkmail[i]'"; line the $checkmail[i] should actually be $checkmail[$i] for it to work though!

Cool!

:)

dreamcatcher

3:19 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mmm...actually it was deleting all the e-mail addresses even if you checked one box.

In the end I figured out to foreach loop.

foreach ($checkmail as $mail)

{

mysql_query("DELETE FROM list WHERE post_email = '$mail'");

}

:)

jatar_k

6:01 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld dreamcatcher,

you could use a combination so you only have one query.

$which = 0;
foreach ($checkmail as $mail) {
if ($which!=0) $maillist .= ",";
$maillist .= "'" . $mail . "'";
$which++;
}
$delq = "DELETE FROM list WHERE post_email in (" . $maillist . ")";
mysql_query($delq);

dreamcatcher

8:46 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the welcome jatar_k and thanks for the alternative method. The foreach loop seems to work fine, but at least I have some more food for thought next time I come across a similar problem.

:)

andreasfriedrich

10:34 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Birdman [webmasterworld.com] wrote at 02:20 AM on Mar. 30, 2003 in message #6 [webmasterworld.com]
>>So that example only queries the db once?

Yes. Thatīs the idea: let MySQL [mysql.com] handle as much as it can. The $checkmail array will only contain the value [w3.org]s of those checkboxes that were actually checked. So there is no need to loop over all those elements and do separate queries.

Neither is there a need to use a loop to build the string to be used in the IN [mysql.com] operator. In my example above implode [php.net] will build the string containing the checked values which is included into the SQL query string by sprintf [php.net].

Andreas

andreasfriedrich

10:41 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To expand on my previous example: If the values are not numeric then the following code will enclose the values for the IN [mysql.com] comparison operator in single quotes (Thanks Adam for your looping example which made me aware of the missing quotes :)):


mysql_query [php.net](
sprintf [php.net](
"DELETE [mysql.com] FROM table WHERE field IN [mysql.com] ('%s')",
implode [php.net]("','", $checkmail)));

Andreas