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
Share on other sites

Change this

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

To just

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.