douchensi8625 2016-09-12 15:29
浏览 19
已采纳

自动更新php中的一行而不使用cron job等,只需​​要纯php和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?

  • 写回答

1条回答 默认 最新

  • dongmeng2687 2016-09-12 15:36
    关注

    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

    <?php
    $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
    if(time()>$expireDate){
        $query = "UPDATE users SET is_reserved = 0 WHERE user_name = '$user'";
    
       //run query
    
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角