Jump to content

[SOLVED] PHP update loop problems


sford999

Recommended Posts

Hi I have the following code to delete messages from a table using checkboxes.

 

<?php
if(isset($_POST['submit']))
		{
			$list = "";
			$counter = 0;
			foreach($_POST['delete'] as $value)
			{
				$list+="'$value$'";
                    if($counter>0)
                    $list+=",";
                    $counter++;
			}

			$sql = "DELETE FROM messages WHERE id IN ($list)";
			$result = mysql_query($sql) or die(sql_error(mysql_error(), $sql));

			if(mysql_affected_rows() >= 1)
			{
				redirect('messages.php', 0);
			}
			else 
			{
				echo mysql_error();
			}
		}
?>

 

The html thats producing the checkboxes

<input name="delete[]" type="checkbox" id="delete[]" value="'.$id.'">

 

However when printing the mysql query its saying this

DELETE FROM messages WHERE id IN (0)

 

Doing a print_r() on $_POST['delete'] says this

Array ( [0] => 33 [1] => 28 [2] => 27 [3] => 26 [4] => 25 ) 

 

However as you can see from the query, the array is not being passed to the query and I can't see where I have gone wrong.

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/169690-solved-php-update-loop-problems/
Share on other sites

Change this

foreach($_POST['delete'] as $value)
            {
               $list+="'$value$'";
                    if($counter>0)
                    $list+=",";
                    $counter++;
            }

To just

$list = implode(', ', $_POST['delete']);

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.