Jump to content

How do I compare a date and insert a row only when current date is newer?


Jeffro

Recommended Posts

This kind of borders on php as much as mysql, but I think the mysql part is what's hanging me up. 

 

This works great in my php page:

 

$query = "INSERT IGNORE INTO postings (title, description, timeStamp) VALUES ( '$title', '$description', STR_TO_DATE('$date', '%a, %d %M %Y %H:%i:%s'))";

 

So now.. for any new records, I want to compare the php $date against the mysql date (column called dateStamp with a type of datestamp). 

 

I tried adding this:

 

$querycheck = "SELECT 1 FROM postings WHERE city='$city' and STR_TO_DATE('$date', '%a, %d %M %Y %H:%i:%s') > timeStamp  LIMIT 1";
if ($querycheck === true) {

$query = "INSERT IGNORE INTO postings (title, description, timeStamp) VALUES ( '$title', '$description', STR_TO_DATE('$date', '%a, %d %M %Y %H:%i:%s'))";
mysql_query($query) or die( "<br>Query string: $query<br>Caused error: " . mysql_error() );

}

 

It seems to be evaluating the expression because..  if I run my php 2x in a row, it inserts about 20 dates the first time.. and 0 the second time.  The problem though..

 

I'll then order by dateStamp desc for that city only... delete the highest date, rerun my php page and it still inserts 0 results, even though $date is higher than any other result in the mysql table at that point. 

 

Any ideas? 

If you're trying to UPDATE data, then use UPDATE, not INSERT.

 

Otherwise, the first thing that jumps to mind that that you're checking for the query being successfully executed, not returned.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.