Help with unset inside of foreach loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cadkins29
Forum Newbie
Posts: 2
Joined: Fri Nov 13, 2009 7:11 pm

Help with unset inside of foreach loop

Post by cadkins29 »

Ok so I'm trying to make an image upload script with the ability for the users to delete their images. The upload works great, the deletion part however only removes the listing from the database, the files are still in the file system.

I've inserted some test echo statements here and there and I can't get the actual file name to be printed to the screen although the ID itself ($aa) seems to be printing to the screen.

When I run the code the filename is not printed although when I had the path printing it was printing. The script seems to run without any error messages so I'm not sure what's going wrong.

It seems my sql statement to select (file) from the table doesn't work. It seems to be empty.
Can someone offer some assistance? I'm sure there must be a way for me to delete the files based on the IDs that are being posted but what am I doing wrong? Currently there's no input filters but I will add that once I get this thing working.

Thanks!
Here's my Code:

Code: Select all

 
if($_POST['delete_photo'])
{ 
    $images_delete = $_POST['delete_photo'];
 
    if($images_delete!=Null)
    { 
        foreach($images_delete as $aa)
        {  
           $sql="select * from `images` where `id` = $aa";
           $image_result=mysql_query($sql);
           print mysql_error();
 
           echo $aa;
           echo "<br />";
           echo "File name:";
           echo $image_result['file'];
 
           @unlink($thumbpath.$image_result["file"]);
           @unlink($medpath.$image_result["file"]);
           @unlink($lrgpath.$image_result["file"]);
 
           $sql="delete from images where id = $aa AND user = $_SESSION[user_id]";
           $result=mysql_query($sql);
           print mysql_error();
 
 
        }
    }
}
 
Again the delete sql statement works and removes the tables from the database but the SELECT statement seems to be returning blank, although I can see the id in the database and have quadruple checked the id numbers, field names, etc. The problem must have something to do with performing a select statement from within a FOREACH loop or something????
cadkins29
Forum Newbie
Posts: 2
Joined: Fri Nov 13, 2009 7:11 pm

SOLVED: Re: Help with unset inside of foreach loop

Post by cadkins29 »

Nevermind... :) I spent hours on this problem, I forgot to fetch the array.. DUH!
Post Reply