php - 根据当前日期和表日期删除mysql表行

标签 php mysql date if-statement sql-delete

我正在尝试制作一个处理研讨会帖子的网页。我希望该页面删除过期日期(或研讨会举办日期)的研讨会 我的 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/

相关文章:

php - 如何停止 ini_set ("max_execution_time", 0);在运行时

mysql - 访问: export to excel with sub table

PostgreSQL 按季节分组(北半球和南半球)

PHP 和 Python 接口(interface)

php - 有没有办法使用 zend 创建电子商务网站?

PHP CURL 没有正确处理编码的返回数据

mysql - 创建ipc端口失败; MySQL Workbench 中的访问被拒绝

MySQL添加分数列到SELECT高分查询

html - 如何在 HTML 日期输入标签中设置日期格式?

javascript - 使用 javascript 或 jquery 从 T 到 mm/dd/yyyy 的日期解析格式