Home  >  Article  >  Backend Development  >  怎么批量增加(datetime)字段值

怎么批量增加(datetime)字段值

WBOY
WBOYOriginal
2016-06-13 13:23:36849browse

如何批量增加(datetime)字段值?
endTime是datetime类型的,用来存放会员到期时间。
userID是会员ID号主键
我想批量给未到期的会员加时间。
代码该怎么写啊?
*********************************
user_data 用户信息表
--------------------
userID-----会员用户编号
userNO-----会员用户帐号
userPW-----会员用户密码(MD5)
regTime-----会员注册时间
endTime-----会员到期时间
********************************

if(isset($_POST['time']))
{
  $time = $_POST['time'];
  $result = mysql_query("SELECT `userID` FROM `user_data` where `endTime`> NOW()");
  $count = 0;
  $countf = 0;
  while($row = mysql_fetch_array($result))
{
$userID = $row['userID'];
$sql = "UPDATE `user_data`
SET `endTime` = dateadd(day,$time,endTime)
where `userID` = '$userID'";
if(mysql_query($sql,$con))
$count++;
else
$countf++;
}
  echo "已给" . $count . "用户增加了时间! " . "失败" . $countf;
}

------解决方案--------------------

PHP code
if (isset($_POST['time'])) {
    $time = $_POST['time'];
    if (mysql_query("UPDATE `user_data` SET `endTime` = DATE_ADD(`endTime`, INTERVAL {$time} DAY) WHERE `endTime` > NOW()")) {
        echo "已给".mysql_affected_rows()."用户增加了时间!";
    }
    else die(mysql_error());
} <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn