-3

我是 php 新手,我有一张桌子

+-----------------------------------------------------------------+
|                              seeker                             |
+----+-------+----------+---------------+-------------------------+
| id | name  | password | register_date | login_date | limit_date |
+----+-------+----------+---------------+------------+------------+
| 01 | ali   |   ****   |  2012-02-08   | 2012-02-09 | 2012-03-09 |
| 02 | hamza |   ****   |  2012-04-10   | 0000-00-00 | 0000-00-00 |
| 03 | sam   |   ****   |  2012-04-15   | 0000-00-00 | 0000-00-00 |
+----+-------+----------+---------------+------------+------------+

当限制日期=当前日期时,我必须应用两个查询一个来删除数据

当用户 login_date 为 '0000-00-00' 且其 (register_date - current date) >= 30 时,第二次删除数据

我的第一个查询正在正确实施

在我的第二个查询中,我通过从当前日期中减去 register_date 来找到天数,如果天数大于或等于 30,则删除该用户。

我的问题是,当任何单个用户返回天数 >= 30 时,所有 login_date ='0000-00-00' 的用户都会被删除。这是我的代码

    //code for  delete user where (current date - register date)>=30
    $qry="select * from seeker where login_date='0000-00-00'";
    $rs= mysql_query($qry, $con);
     $res=mysql_num_rows($rs);
    echo "total rows of zero dates "."".$res;
    echo "<br>";
    if($res!=0)
    {
        echo "user exist of date='0000-00-00'";
        echo"<br>";
        $qqq="select * from seeker where login_date='0000-00-00'";
            $rsq= mysql_query($qqq, $con);
            while($resq=mysql_fetch_array($rsq))
            //echo $resq;
        {
          $r=$resq['c_date'];
          echo $r;
          $reg_date = strtotime($r); 
          echo "<br>";

    //current date
        $c= date("Y-m-d");
        echo $c;
        //echo "current date".""."<br>";
        $current_date = strtotime($c); 
        echo "<br>";

         //date difference
          $datediff = $current_date - $reg_date;
          $days = floor($datediff/(60*60*24));
          echo "days"."".$days;
          echo "<br>";

        if($days>=30)
        {
        $qry1="delete from seeker where $days = '30'";
        $rs1= mysql_query($qry1, $con);
        echo $qry1;
        echo "<br>";
        echo "<br>";
        }
      }

}
// code for delete user where current date = limit date
$qry3="select * from seeker where current_date = limit_date";
$rs3= mysql_query($qry3, $con);
$res3=mysql_num_rows($rs3);
if($res3!=0)
{

        $q= "delete from seeker where current_date = limit_date";
        $rq=mysql_query($q, $con);
        echo "$q";
        echo "<br>";
}

请告诉我哪里出错了。我只想删除天数 >=30 的用户,但它删除了 login_date ='0000-00-00' 的所有用户

for example if current date is 2012-01-10. it will not delete user
if current date is 2012-05-10.. it must delete only "hamza" but it delets "sam" too 

请帮助我,我是 php 的学生和新手。提前致谢

4

2 回答 2

1

请在查询中尝试DATEDIFF(time1 ,time2)函数和NOW()函数,如下所示

DELETE FROM seeker WHERE DATEDIFF( NOW( ) , `register_date` )  >= 30 AND 
`login_date` = '0000-00-00'

谢谢。

于 2012-05-10T06:26:24.557 回答
0

使用以下查询,

DELETE FROM `seeker` 
WHERE `register_date`>DATE_SUB(NOW(), INTERVAL 30 day)
于 2012-05-10T06:14:45.597 回答