Jump to content

Deleting records based on an expire date?


Solarpitch

Recommended Posts

Hello!

Ok . . I am developing a golfing website that allowes people to upload adds of there equipment they want to sell. They get a choice to have the add online for 1, 3, 5, 7, or 14 days.

If the user selects thier add to run for lets say 5 days . . is thier anyway I can delete the record after the 5 days is up? Can that be done automatically?

So if the add is uploaded on the 2nd December . . how do I add 5 days onto that for it to expire? ummm . . this one has really got me thinking!

Cheers!
Link to comment
https://forums.phpfreaks.com/topic/26364-deleting-records-based-on-an-expire-date/
Share on other sites

Upon creating a new add, store the current time (example mysql NOW() will do) as [color=blue]date_added[/color] and the number of [color=blue]days[/color] to expire as number (like 5) (blue coloured names referring to my below example on col-names)

Then you can run a query like this example in your script listing all adds, just run it on the top before querying to display adds:

[code]

<?php

$sql = mysql_query("delete from table where date_added < date_sub(now(), interval (days) day)") or die(mysql_error());

?>

[/code]

This is a solution if you cannot use cron for the job.
Now, each time a visitor requests the file to display all ads, expired ads will be deleted first.

Archived

This topic is now archived and is closed to further replies.

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