我正在尝试制作一个处理研讨会帖子的网页。我希望该页面删除过期日期(或研讨会举办日期)的研讨会 我的 table 有 3 个日期槽,一个代表月份,一个代表日期,一个代表年份。表中均为整数。
if(date("Y") > $info['year']) //if the year is old
{ $sql = "DELETE FROM seminars WHERE ID = '$ID'";
mysql_query($sql); } //then delete this seminar
else if(date("Y") == $info['year'] && date("n") > $info['month']) //if we are in the year of the seminar, but the month is now old
{ $sql = "DELETE FROM seminars WHERE ID = '$ID'";
mysql_query($sql);} // then delete this seminar
else if(date("Y") == $info['year'] && date("n") == $info['month'] && date("j") > $info['day']) //if we are in the year of the seminar and the month of the seminar, but the day is old
{ $sql = "DELETE FROM seminars WHERE ID = '$ID'";
mysql_query($sql); } // then delete this seminar
else // if there is no reason to delete the seminar then print out this seminar
如上所示,我尝试使用年月日的系统时间并比较每个时间以查看是否有旧的内容。然而,它永远不会删除研讨会。
现在,$info
是表行,因为该语句位于 while 循环中,因此它可以抓取表中的每一行。但即使删除语句不起作用,它也不会显示研讨会,对吗?最后的 else 语句是显示研讨会的开始位置。
如果有人知道更好的方法来做到这一点,我将非常感激。我已经为此苦苦挣扎了一段时间。
最佳答案
虽然最好重新设计表以包含单个日期列,但您可以使用现有架构,如下所示:
if (date("Y-m-d") > sprintf("%04d-%02d-%02d", $info['year'], $info['month'], $info['day'])) {
$sql = "DELETE FROM seminars WHERE ID = '$ID'";
mysql_query($sql);} // then delete this seminar
}
关于php - 根据当前日期和表日期删除mysql表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19055806/