Grafity Grafity - 1 year ago 62
MySQL Question

automatically updates a row in php without using cron job etc, just pure php and mysql

I have a table this is whats inside of it:

id item_id user_name quantity date_expired is_reserved
1 2 jimmy123 5 2016-9-15 1
2 1 kilmer 2 2016-9-17 1

I want to automatically update the is_reserved in jimmy123's transaction record from 1 to 0 when date_expired reaches sept. 15, 2016, how will I be able to do that automatically in PHP and MYSQL?

Answer Source

You do not really need a cron job, as it can be done when the visitor, or any visitor visits. In 99% of cases, this should be suitable, even if it seems a little inefficient - its how its done. Unless the is an underlying reason? You have not given any information or code about how the username and timestamp of interest is determined, so in my example I will hard code them assuming you will replace the values later.

So you can get the epoche time, of that date, 1473897600. This is how you should store your dates, and then convert it to something readible if your outputting it.

Then have a script that looks something like this

$user = "jimmy123";
$query = "SELECT date_expired FROM users WHERE user_username = '$user'"; //assumes date_expired is now a timestamp format

$expireDate = ... //run query and get single result
    $query = "UPDATE users SET is_reserved = 0 WHERE user_name = '$user'";

   //run query


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download