如何将变量的值传递给mysql foreach行

时间:2013-04-25 18:54:05

标签: php mysql sql

 $sql = mysql_query("SELECT * From orders");

 while ($row = mysql_fetch_array($sql))
 {
     $datef = strtotime($row['Date1']);
     $dates = strtotime($row['Date2']);
     $rprice = $row['Pprice'];

     $datediff = floor(($datef + $dates ) / 86400);
     $total = $datediff * $rprice;

     echo $total; 
 }

在该代码之后,我想更新一个表格,并在列中插入每行的价格。至于我回声$total我只回到了一排......

2 个答案:

答案 0 :(得分:1)

尝试此操作

  function total($date1,$date2,$price){

  $datedif = floor(($date1 + $date2 ) / 86400);
 $total = $datedif * $price ;
  return $total ;

 }
 $sql=mysql_query("SELECT * From orders");

 while ($row = mysql_fetch_array($sql)) 

{   $datef = strtotime($row['Date1']);
    $dates = strtotime($row['Date2']);
    $rprice=$row['Pprice'];
    echo total($datef,$dates,$rprice).'<br />' ;

 }

编辑:

insert into ammounts (Total_Cost) VALUES ('total($datef,$dates,$rprice)')
WHERE id = '$row["id"]'

答案 1 :(得分:0)

假设您的order_id表中有total列作为PRIMARY KEY和orders列。

 while ($row = mysql_fetch_array($sql)) 

{   $datef = strtotime($row['Date1']);
    $dates = strtotime($row['Date2']);
    $rprice=$row['Pprice'];
    $total=total($datef,$dates,$rprice);
    $query=mysql_query("UPDATE orders SET total = $total WHERE order_id = '$row[order_id]'") or die(mysql_error());
    echo $total.'<br />' ;
 }