Problem with mysql query inside foreach loop

Discussion in 'PHP' started by youradultworld, Nov 13, 2009.

  1. #1
    I made a image upload script and that works and I'd like users to be able to delete the images. I made it so the rows are removed from the database but when I try to perform a select sql statement inside of the existing foreach loop (so that I can get the filename and unlink it from the file system) the query continues to turn up blank and I'm really confused no matter what I try doing it seems to be doing the same thing. Anything I'm overlooking??

    Here's my code:

    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();
    				           
    
              				}
          			}
    }
    PHP:
    Thanks in advance!!

    Ps. As I said the delete query works fine, but the select statement before the delete statement seems to not return anything so therefore nothing is unlinked. I've tried adding some temp echo statements to show me what's working and not and that's how I know the result turns up blank.
     
    youradultworld, Nov 13, 2009 IP
  2. youradultworld

    youradultworld Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SOLVED: Nevermind... :) I spent hours on this problem, I forgot to fetch the array.. DUH!
     
    youradultworld, Nov 13, 2009 IP