简体   繁体   中英

Deleting a mysql table row based on current date and table date

I am trying to make a webpage that deals with seminars postings. I would like the page to delete a seminar that is past its expiration date (or the date that the seminar occurs) My table has 3 date slots, one for month, one for day, one for year. They are all integers in the table.

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

As can be seen above I tried to use the system time for year month day and compared each one to see if anything was old. However, it is never deleting the seminars.

Now, $info is the table row, because this statement is in a while loop so that it can grab every row from the table. But even if the delete statement wasn't working, then it wouldn't display the seminar right? The last else statement is where displaying the seminar begins by the way.

if anyone knows a better way to do this I would appreciate it a lot. I have been struggling with this for a while now.

Although it would be better to redesign your table to contain a single date column, you can use your existing schema like this:

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
}

You don't have to have three columns each for a month, day and year. Instead you should have only one column which has the column type DATETIME . This will make your job much easier. You only need to match with the current date.

You can do something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM