xtrafile Posted February 19, 2008 Share Posted February 19, 2008 Hi there, I have this code: $expires = expires-" . $coupon->id . "; if ($expires < date("m/d/y")) { $expires = "true"; mysql_query("DELETE FROM `coupon`"); } When i run the page it should delete from the query all the old listings that no longer exist. How can I update this? Thanks! Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/ Share on other sites More sharing options...
mem0ri Posted February 19, 2008 Share Posted February 19, 2008 You need to put a WHERE clause on your DELETE statement for SQL or you're going to delete all entries in the coupon table. The WHERE clause should clearly identify one (or several) rows as those you want to have deleted. From the block of code you have given, it is impossible to guess what those identifiers might be. Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/#findComment-470161 Share on other sites More sharing options...
xtrafile Posted February 19, 2008 Author Share Posted February 19, 2008 Thanks. Does this look right? $expires = expires-" . $coupon->id . "; mysql_query("DELETE FROM `coupon` WHERE `expires` "); What goes in the blank space you think? I have no clue what I'm doing.. Thanks! Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/#findComment-470165 Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 a few things, you have syntax errors, cannot use formmated date as an integer, and you will delete all rows. to fix your code to what i think you mean: if (strtotime($expiretime) < time()) { $expired = "true"; mysql_query("DELETE FROM `coupon` WHERE `id`='$coupon->id';"); } Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/#findComment-470166 Share on other sites More sharing options...
xtrafile Posted February 19, 2008 Author Share Posted February 19, 2008 Alright I changed the code to that. However, there is a problem with that. The mysql_query deletes everything, because id always equals $coupon->id. So if the if statement is true, the WHERE will either way delete all the rows. Thanks Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/#findComment-470176 Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 mysql_query("DELETE FROM `coupon` WHERE `exp`<='".time()); the mysql table will need a field called exp which will need to be a unix timestamp (should be set when the entry is added) Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/#findComment-470181 Share on other sites More sharing options...
xtrafile Posted February 19, 2008 Author Share Posted February 19, 2008 Thanks. My new code: $expires = expires-" . $coupon->id . "; if (strtotime($expires) < time()) { mysql_query("DELETE FROM `coupon` WHERE `exp`<='".time());} Looks right? Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/#findComment-470193 Share on other sites More sharing options...
uniflare Posted February 19, 2008 Share Posted February 19, 2008 mysql_query("DELETE FROM `coupon` WHERE `exp`<='".time()); this is all you should ever need tbh, i dont get what your trying to do with this: $expires = expires-" . $coupon->id . "; the strtotime() function only accepts formatted dates and times, eg: Feb 16 08 putting that $expires = variable will break this code as "expires-" is not anything to do with dates or times. Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/#findComment-470218 Share on other sites More sharing options...
xtrafile Posted February 24, 2008 Author Share Posted February 24, 2008 Thanks. mysql_query("DELETE FROM `coupon` WHERE `exp`<='".time()); appears to be syntactically incorrect... Is <='".time() supposed to be like that? There's an ' which doesn't have a closing anywhere.. Thanks Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/#findComment-475290 Share on other sites More sharing options...
uniflare Posted February 25, 2008 Share Posted February 25, 2008 very sorry my mistake , you are correct i missed an ' off the end mysql_query("DELETE FROM `coupon` WHERE `exp`<='".time()."'"); Link to comment https://forums.phpfreaks.com/topic/91802-deleting-oldexpired-listings/#findComment-475864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.