Link to home
Start Free TrialLog in
Avatar of spradjinata
spradjinata

asked on

proper use of unlink() for deleting record and image

hi...is there anyone who can show me the proper code for using unlink() ?  I have a table in the database with image path on it.

so it looks like this

listingID  | listingTitle | desc                        | image                  | date
1            |cheap psx  | good and cheap psx  | c:/images/psx.jpg | 8/5/2005

I wanted to delete this listing  once it get past its expiry date(30 days from the listing) The problem is the image would still be in the database...I have read a thing or two about unlink...but i'm not sure of its proper usage.

I am using php, mysql, apache and windows 2000. Thank you.

Avatar of German_Rumm
German_Rumm

Hi spradjinata,

You have a full path in your database. That's kinda good. Not portable, but good for your case.
First, you get all of your "expired" images list.
$sql = 'SELECT * FROM table WHERE NOW() > DATE_ADD(`date`, INTERVAL 30 DAY)';
// assuming your `date` column is of DATETIME type
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
   $deleteSuccessful = @unlink($row['image']); // delete an image
   if ($deleteSuccessful) { // if delete successfull
        $sql = 'DELETE FROM table WHERE image = "'.$row['image'].'" AND listingID = "'.$row['listingID'].'"';
        mysql_query($sql); // delete record from database
    }
}

---
German Rumm.
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of spradjinata

ASKER

I want to delete both the record and the image. both solution looks great. I will award the points after I tried it. Thanks very much.
thank you ldbkutty....your answer is perfect...thanks.....sorry german rumm...your answer doesn't work...i wanted to give you points but i can't split it....(the split point is missing)......thanks though.
>> thank you ldbkutty....your answer is perfect...thanks.....
Thanks.

>> (the split point is missing)
!!!!

If you wish, I can reopen the question or split the points.