2013-09-27 45 views
0

我正在尝试制作一个网页,讨论研讨会发布内容。我希望该页面删除已过期(或研讨会发生日期)的研讨会 我的表格有3个日期插槽,一个为月份,一个为日期,一个为年份。他们都是桌子上的整数。根据当前日期和表格日期删除mysql表格行

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循环中,因此它可以抓取表中的每一行。但即使删除语句​​不起作用,那么它也不会显示研讨会的权利?最后的陈述是展示研讨会的地方。

如果有人知道更好的方法来做到这一点,我将不胜感激。我一直在为此苦苦挣扎了一段时间。

+0

“我希望页面删除超过其到期日期(或研讨会发生日期)的研讨会“是否有具体理由说明为什么不使用MySQL DATETIME列类型? – u54r

+0

我不知道如何将我从表单中获取的日期转换为该数据类型。 – ForgottenOne

+0

'$ year。 ' - '。 $月。 ' - '。 $ date'给你一个有效的MySQL'DATE'。 – TheWolf

回答

0

您不必每个月,每天和每年都有三列。相反,您应该只有一列具有列类型DATETIME。这将使你的工作更容易。你只需要匹配当前的日期。

1

虽然这将是更好地重新设计你的表包含一个日期栏,您可以使用您现有的模式是这样的:

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 
} 
0

你可以这样做:

$infodate = strtotime(implode('-', array($info['year'], $info['month'], $info['day']))); 

if (time() > $infodate) { 
    mysql_query("DELETE FROM seminars WHERE ID = '$ID'") 
} else { 
    //print out this seminar 
}