Hi could someone help me out with this code please?
I have commented what it should do(or ewhat I want it to do!)

if(isset($_POST['remove'])){
      $chk = (array) $_POST['remove'];
      $p = implode(',',array_keys($chk)); 
      $q = mysql_query("DELETE FROM image_gallery WHERE id IN ($p)");
      $t = mysql_query("SELECT filename from image_gallery WHERE id IN ($p)"); //filename is the path of the file.. eg. image/siteimages/logo.gif
      $s = mysql_query("DELETE FROM thumbs_gallery WHERE id IN ($p)");

      if ($sql = $s){
        $q;
        unlink($t); //this isnt doing anything at all
        header( 'Location: lee_remove.php' ) ;
      }

The entries get deleted from both tables ok, but the image file does not get deleted from the dir.
Can anyone point out where im going wrong??

Thanks for looking...............

Recommended Answers

All 8 Replies

Member Avatar for diafol

$sql doesn't exist. $s returns boolean (success/failure). What is the point of line 9?

If all the images are in the same folder, you can dispense with the '$t' query.
Anyway, $t is just a resource - not a string (file location).

You need to call mysql_fetch_assoc() to get the path.

First this is wrong:

if ($sql = $s){

here you are assignin a value, not comparing, to compare use == or for boolean ===, so rewrite it to:

if ($sql == $s){

Second, I don't see $sql declared anywhere, you will get an error here.
Third, you have to extract the filename from the query:

if(mysql_num_rows($t) > 0)
{
    # loop results
    while($row = mysql_fetch_object($t))
    {
        unlink('/path/'.$row->filename); # and extension if this is saved a part
    }
    header('Location: lee_remove.php');
}

bye!

pass your file name in a varialbe like
 $t = mysql_query("SELECT filename from image_gallery WHERE id IN ($p)");
$url=mysql_fetch_array($t);
$image=$url['filename'];  //what field you use in db type in the url index
then do
unlink($image);
this will delete the file from directory....

Hi thanks guys.

This is what I have now

if(isset($_POST['remove'])){
      $chk = (array) $_POST['remove'];
      $p = implode(',',array_keys($chk)); 
      $q = mysql_query("DELETE FROM image_gallery WHERE id IN ($p)");
      $s = mysql_query("DELETE FROM thumbs_gallery WHERE id IN ($p)");
      $t = mysql_query("SELECT * FROM image_gallery WHERE id IN ($p)");
      $url=mysql_fetch_array($t);
      $image=$url['filename'];  

      if ($s){

        unlink($image); 


      }
      else{
         echo 'There has been a problem. Go back and try again';
         echo "<br />";
         echo "<a href='lee_remove.php'>Back</a>";
      }
   }
   else{
         echo 'There are no images in the gallery';
         echo "<br />";
         echo "<a href='gallery_upload.php'>Add Images</a>";
      }

But I am getting this error
' Warning: unlink(): Invalid argument in C:\wamp\www\diamondback\lee_remove2.php on line 23' at unlink($image)

Any ideas?

Echo what is stored in $image. I'm pretty confident it's an empty string or null, because you first delete the id's in $p and then try to select them.

i am not test it but try this code it will work or not

if(isset($_POST['remove'])){
      $chk = (array) $_POST['remove'];
      $p = implode(',',array_keys($chk)); 

      $t = mysql_query("SELECT * FROM image_gallery WHERE id IN ($p)");
      $url=mysql_fetch_array($t);
      $image=$url['filename'];  
      if ($t){

        unlink($image); 
        $q = mysql_query("DELETE FROM image_gallery WHERE id IN ($p)");
      $s = mysql_query("DELETE FROM thumbs_gallery WHERE id IN ($p)");
      }
      else{
         echo 'There has been a problem. Go back and try again';
         echo "<br />";
         echo "<a href='lee_remove.php'>Back</a>";
      }
   }
   else{
         echo 'There are no images in the gallery';
         echo "<br />";
         echo "<a href='gallery_upload.php'>Add Images</a>";
      }

Thats what was happening, I was deleting the id first then tryin to unlink it after!

art18, your code worked a treat! Thank you.

Thanks everyone that reploied to this.

Glen...

you are welcome glenrogers i m glad to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.