逐个更新mysql中的多行

时间:2013-11-29 22:52:07

标签: php mysql

我正在尝试在一个查询中更新多行?

<?php
//error_reporting(0);
include("_models.header.php");
$model= $_GET["modelname"];
$timestamp= date("Y-m-d h:i:s");
mysql_query("UPDATE groupshows SET showended='".$timestamp."' WHERE model='".$model."' AND showended='0'");
mysql_query("UPDATE groupshows SET ended='1' WHERE model='".$model."' AND customer='0'");
mysql_query("UPDATE groupshows SET started='0' WHERE model='".$model."' AND customer='0'");


//udregn show cost
$result=mysql_query("SELECT * FROM groupshows where model ='".$model."' AND  customer='0'");
while($row=mysql_fetch_array($result))
$id=$row["groupshowid"];

$result=mysql_query("SELECT * FROM groupshows where groupshowid='".$id."' and cost='0' limit 1");
while($row=mysql_fetch_array($result))
{
    $customer=$row["customer"];
    $cost=$row["cost"];
    $startime=$row["showstarted"];
    $endtime=$row["showended"];
}
if ($cost=='0'){

    $to_time = strtotime($endtime);
//echo "endtime $to_time<br>";
$from_time = strtotime($starttime);
//echo "start $from_time<br>";
$timespend=round(abs($to_time - $from_time) / 60,2);
$timespend2=date("Y-m-d h:i:s",$timespend);
//echo "timespend $timespend<br>";
$cost=number_format($timespend,0)*0.39;
$cost2=$cost/100;
//echo "cost $cost<br>";
//echo "Show started $starttime Show ended $timestamp<br>";
$Earn=number_format($cost, 2)/100;
//echo "<b>total cost $ $cost2</b>";

mysql_query("UPDATE groupshows SET cost=cost + '".$cost."' Where customer='".$customer."' AND cost='0'");
$result++;
}

我无法在不同客户的开始时间和结束时间中获取时间戳,它将更新第一行但不会转到下一行然后我认为模型的开始和结束时间不是客户

在数据库中: id groupshowid model customerid客户标志启动userstarte
17 BMli3Ggb ilovecock 0 0 3 0 0

  显示开始showended费用用户结束 2013-11-29 04:25:04 2013-11-29 04:29:37 1 0 1

18 BMli3Ggb ilovecock 0 tomdodi 0 0 1

2013-11-29 04:25:04 2013-11-29 05:01:24 8.97 0 0

19 BMli3Ggb ilovecock 0 test 0 0 0 2013-11-29 04:25:04 2013-11-29 04:26:37 0 0 0

1 个答案:

答案 0 :(得分:0)

在我看来,你需要移动一些项目,即if ($cost =='0') {块进入第二个while循环。否则,您只会更新找到的最后一条记录。另外,我认为以下内容可能更符合您的意图:

<?php
//error_reporting(0);
include("_models.header.php");
$model= $_GET["modelname"];
$timestamp= date("Y-m-d h:i:s");
mysql_query("UPDATE groupshows SET showended='".$timestamp."' WHERE model='".$model."' AND showended='0'");
mysql_query("UPDATE groupshows SET ended='1', started='0' WHERE model='".$model."' AND customer='0'");

//udregn show cost
$result=mysql_query("SELECT * FROM groupshows where model ='".$model."' AND  customer='0'");
while($row=mysql_fetch_array($result))
{
  $id=$row["groupshowid"];

  $result2=mysql_query("SELECT * FROM groupshows where groupshowid='".$id."' and cost='0' limit 1");
  while($row2=mysql_fetch_array($result2))
  {
    $customer=$row2["customer"];
    $cost=$row2["cost"];
    $startime=$row2["showstarted"];
    $endtime=$row2["showended"];

    if ($cost=='0') {

      $to_time = strtotime($endtime);
      //echo "endtime $to_time<br>";
      $from_time = strtotime($starttime);
      //echo "start $from_time<br>";
      $timespend=round(abs($to_time - $from_time) / 60,2);
      $timespend2=date("Y-m-d h:i:s",$timespend);
      //echo "timespend $timespend<br>";
      $cost=number_format($timespend,0)*0.39;
      $cost2=$cost/100;
      //echo "cost $cost<br>";
      //echo "Show started $starttime Show ended $timestamp<br>";
      $Earn=number_format($cost, 2)/100;
      //echo "<b>total cost $ $cost2</b>";

      mysql_query("UPDATE groupshows SET cost=cost + '".$cost."' Where customer='".$customer."' AND cost='0'");
    }
    // result is a mysql result resource (and can't be incremented like this)
    // $result++;
  }
}

与问题无关,但第二和第三个UPDATE语句可以合并为一个查询:UPDATE groupshows SET ended='1', started='0' WHERE model='".$model."' AND customer='0'");

这是gist of your original code vs. this change