Forum Moderators: coopster

Message Too Old, No Replies

Another Multiple Check box problem

         

riscphree

3:07 am on Jan 15, 2008 (gmt 0)

10+ Year Member



I've done a search and read through some of the posts and threads that I've seen so far, but nothing seems to be working for me.

What I'm trying to do is delete items in a mysql database that are displayed with a simple checkbox. Here is the form that is generated.

<form action="" method="post">

sql query here

print"<input type="checkbox" class="delete-checkbox" value="{$row['id']}" name="deletebox[]" />";

<input type="submit" name="submit" value="delete messages" />
</form>

Here is the php that I'm using when its POSTED

if(isset($_POST['submit'])) { 
foreach($_POST['deletebox'] as $deletebox1){
mysql_query("DELETE FROM private_messages WHERE id='$deletebox1'") or die(mysql_error());
}
}

However, nothing happens, which seems to be something with the array, but I can't figure it out. I've tried putting something to print out what is in the array that is passed, but it never prints anything out, like its blank. And I'm positive there are values in teh deletebox[] value when you check it.

dreamcatcher

1:50 pm on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi riscphree and welcome to Webmaster World. :)

Firstly, there is no way this line can parse correctly:

print"<input type="checkbox" class="delete-checkbox" value="{$row['id']}" name="deletebox[]" />";

You have double quotes which should generate a parse error as you are using double quotes in your string.

For the array, the code seems fine. Are you sure that there are values? Try using print_r($_POST['deletebox']) when the form submits.

If the array is holding ID`s, there is no need for a loop:


if(isset($_POST['submit'])) {
if (!empty($_POST['deletebox'])){
mysql_query("DELETE FROM private_messages WHERE id IN (".implode(",",$_POST['deletebox']).")") or die(mysql_error());
}
}

dc