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? 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.