I would like to know how can I auto delete record when date is expired, I’m creating a coupon website. I need to delete from my mysql database all the expired coupons. I read somewhere that I can create a crontab but I have any idea how to do it. Any Help for the script will be very helpful.
Check it out here: http://www.unixgeeks.org/security/newbie/unix/cron-1.html
Thank you for quick reply, very helpful. I still have to figure out how to create delete.php file to do the job. I’m not expert in php any help will be very appreciate.
This is an example of a delete.php file you could use:
<?php include('inc/connect.php'); $db = mysql_connect("$host", "$user", "$pass") or die("Error connecting to database"); mysql_select_db("$dbname",$db); $sql = "DELETE FROM `coupons` WHERE `coupon_expdate` <= CURDATE()"; $result = mysql_query($sql) or die("Error connecting to database!"); ?>
The $sql command will delete from the table “coupons” all the coupons which have expiry dates that have passed the current date(=) or previous dates (<).
Hope this helps you!
Thank you so much for your help your are genius.
I try the $sql command
$sql = “DELETE FROM
expires <= CURDATE()”;
instead to delete from the table “coupons” all the coupons which have expiry dates that have passed the current date(=) or previous dates (<), the command delete every row with a date on it even not expire.
My “expires” row in my database is set as VARCHAR because sometime instead to put a date I put some text like “Unknown”.
Any idea what it can be?
I did a test changing my “expires” row in my database who was set as VARCHAR to DATE but now when I enter date (12/08/2006 for UK) in my database result appear 0000-00-00…
I got it work like a charm now, I change my date data row to DATE and I setup a way to enter the date in the database 2006-08-08 and show screen 08/08/2006.
I test the delete.php script work perfect I just have to create my crontab now.
I’m so happy thank you so so much everyone