【问题标题】:php: script updates fields prematurely?php:脚本过早更新字段?
【发布时间】:2012-12-13 18:05:54
【问题描述】:

我正在制作一个拍卖类型的网站,您可以想象它应该在产品过期时将其除名。过期只是存储为 MySQL 数据类型 DATE。

所以 check_items.php:

<?php
function check_items()
{
$con = mysql_connect('localhost','heh','heh');
mysql_select_db('heh_db',$con);
$q = mysql_query("select last_check from ran_last",$con) or die("Check ran_last 1");
$r = mysql_fetch_assoc($q);
//if((time()-strtotime($r['last_check'])) >(60*60*17))//check only once every 17 hours
if(true)
{
    $q2 = mysql_query("select * from Item where expired=0");
    $remove = array(); $count=0;
    while($row = mysql_fetch_assoc($q2))
    {
        if(strtotime($row['time_expire'])<time())
        {
            echo("strtotime: ".strtotime($row['time_expire'])." time: ".time());
            $remove[$count] = $row['ItemID'];
            $count++;
        }
    }
    mysql_free_result($q2);
    foreach($remove as $next)
    {
        echo($next);
        $q3 = mysql_query(sprintf("select * from Item where ItemID='%s'",$next)) or die("check items outer query foreach");
        $r3 = mysql_fetch_assoc($q3);
        $q4 = mysql_query(sprintf("update Item set expired='1' where ItemID='%s'",$r3['ItemID']));
        if(isset($r3['bidderID']))
        {
            $f1 = mysql_query(
                sprintf("insert into notifications(userID,item_name,ItemID,type,info) values('%s','%s','%s','%s','%s')",
                $r3['bidderID'],
                $r3['item_name'],
                $r3['ItemID'],
                "BUY",
                sprintf("You have won the bidding for this item. Contact the <a href=\"pm.php?ID=%s&&expired_item=%s\">seller</a> for details",
                    $r3['userID'],
                    $r3['ItemID'])
                ),$con
            );
            $f2 = mysql_query(
                sprintf("insert into notifications(userID,item_name,ItemID,type,info) values('%s','%s','%s','%s','%s')",
                $r3['userID'],
                $r3['item_name'],
                $r3['ItemID'],
                "SELL",
                sprintf("<a href=\"pm.php?ID=%s&&expired_item=%s\">User</a> has won the bidding for your item. You are encouraged to contact each other",$r3['bidderID'],
                    $r3['ItemID'])
                ),$con
            );
        }
        else
        {
            $f1 = mysql_query(
                sprintf("insert into notifications(userID,item_name,ItemID,type,info) values('%s','%s','%s','%s','%s')",
                $r3['userID'],
                $r3['item_name'],
                $r3['ItemID'],
                "SELL",
                sprintf("Unfortunately no one bid on your item. You can view expired items from your userpage and re-upload",
                    $r3['userID'])
                ),$con
            );
        }
        mysql_free_result($q3);
    }
    $done = mysql_query("insert into ran_last values()");
}
mysql_free_result($q);
}
?>

功能齐全的脚本会存储上次更新的时间,并且应该每 17 小时运行一次。现在,只要调用该函数,它就会运行。基本上每当我列出一个产品时,它就会自动被除名。

【问题讨论】:

  • 不要在新代码中使用mysql_*,因为它已被弃用,请改用PDO mysqli_*
  • 我的学校承诺在 12 月 23 日学期结束后更新到 PHP5。

标签: php time strtotime php4 unix-timestamp


【解决方案1】:

呃,你为什么要这样?你可以在 PHP 中进行过滤,省去很多麻烦:

SELECT ItemID
FROM Item
WHERE (expired = 0) AND (time_expire < (SELECT last_check FROM last_ran))

另外,当您进行真正的到期检查时,您甚至没有检查此值:

    if(strtotime($row['time_expire'])<time())
                                      ^^^^^^--- shouldn't this be $r['last_check']?

【讨论】:

  • 哇,这真的很容易。老实说,我认为子查询是非法的。 $r['last_check'] 存储上次运行此脚本的时间。 if 语句告诉我该产品是否已准备好退市。
  • 子查询是合法的,但有限制。特别是limit(双关语)。您不能直接在子查询中使用限制,但可以通过将限制放在子子查询中并在中间子查询周围包装 select ** (...) 来伪造。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
  • 2014-07-26
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
相关资源
最近更新 更多