0

我希望能够总结页面中显示的所有收入,并且每次我将其他数据添加到收入列时它都会自动求和:

以下是我的代码:

<?php 
   require_once('Connections/connect.php');
   $id_customer = mysql_real_escape_string($_GET['id_customer']);                                   
   $sql_PK = "SELECT * FROM tbl_delivery_details WHERE tbl_customer_id_customer =    {$id_customer}";
   $PK = mysql_query($sql_PK, $connect);
   if ( mysql_error() ) {
      die ( mysql_error());
   }
   $row_PK = mysql_fetch_assoc($PK);            
   $customer_name = $row_PK['tbl_customer_id_customer'];
   $customer_name = mysql_real_escape_string($customer_name);                       

   $sql = "SELECT tbl_customer.customer_name, 
       tbl_delivery_details.delivery_details_route, 
       tbl_delivery_details.delivery_details_destination, 
       tbl_delivery_details.delivery_details_van_no, 
       tbl_delivery_details.delivery_details_waybill_no, 
       tbl_delivery_details.delivery_details_charge_invoice,
       tbl_delivery_details.delivery_details_revenue,
       tbl_delivery_details.delivery_details_strip_stuff,
       tbl_delivery_details.delivery_details_date           
       FROM tbl_customer, tbl_delivery_details          
       WHERE tbl_customer.id_customer = tbl_delivery_details.tbl_customer_id_customer 
       AND tbl_customer.id_customer = '{$customer_name}'";

       $res = mysql_query($sql) or die(mysql_error());
       $row = mysql_fetch_array($res);
       $sum = 0;

?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/x   html">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Customer Revenue</title>
    <link rel="stylesheet" type="text/css" href="qcc.css"/>
    </head>             
    <body>              
    <table border="1">
      <tr>
         <th>Reveneu</th>                                                 
      </tr>
      <?php do { ?>
          <tr>                          
              <td><?php echo $row_PK['delivery_details_revenue'];?></td>                                                       
      </tr>
      <?php } while ($row_PK = mysql_fetch_assoc($PK));?>
      <?php { ?>
        <?php  $sum+=$row_PK['delivery_details_revenue'] ?>
      <?php } ?>

      </table>

      <?php echo $sum; ?>
    </body>
  </html>

当我加载页面时, echo $sum 始终为零如何正确总结我所做的列,如果我向其中添加其他数据,它将自动求和:

4

3 回答 3

2

与其在 PHP 中将收入值相加,不如让 MySQL 在查询中为您做这件事?

$sql = "SELECT SUM(tbl_delivery_details.delivery_details_revenue) as revenue, 
    tbl_customer.customer_name, 
    tbl_delivery_details.delivery_details_route, 
    tbl_delivery_details.delivery_details_destination, 
    tbl_delivery_details.delivery_details_van_no, 
    tbl_delivery_details.delivery_details_waybill_no, 
    tbl_delivery_details.delivery_details_charge_invoice,
    tbl_delivery_details.delivery_details_revenue,
    tbl_delivery_details.delivery_details_strip_stuff,
    tbl_delivery_details.delivery_details_date

FROM tbl_customer, tbl_delivery_details 

WHERE tbl_customer.id_customer = tbl_delivery_details.tbl_customer_id_customer 
AND tbl_customer.id_customer = '{$customer_name}'";

然后在你看来,只是呼应 SUM 数字......

echo $row_PK['revenue'];
于 2013-01-04T07:06:37.997 回答
0

好吧,我脑子里没有一个 PHP 解释器来运行你的代码。所以,只有一些我能发现的东西

首先,您的第一个查询中存在 SQL 注入。将您的变量转换为整数

$id_customer = intval($_GET['id_customer']);             

或将其视为查询中的字符串

$sql_PK = "SELECT * FROM tbl_delivery_details WHERE tbl_customer_id_customer = '$id_customer'";

或者 - 更好的是 - 使用一些数据库包装器,允许您使用占位符来表示查询中的实际数据。

接下来,您的查询非常难以阅读。
如果您的字段名称不干扰,则没有理由使用table.field符号。
如果您想要表中的大部分字段,还可以使用短地别名并考虑使用 *:

$sql = "SELECT SUM(delivery_details_revenue) as revenue, 
               customer_name, tbl_delivery_details.* 
        FROM tbl_customer, tbl_delivery_details 
        WHERE id_customer = tbl_customer_id_customer 
              AND id_customer = '$customer_name'";

顺便说一句,在编辑您的查询时,我注意到命名不一致:id_customer = '$customer_name'. 不要将自己与错误的变量名混淆。如果是 id,则称它为“id”,而不是“name”

而且我在第一个查询中根本看不到任何意义,如果id_customer等于tbl_customer_id_customer. 我认为你需要简化你的代码——我相信它的复杂性是你没有得到结果的主要原因。

从非常简单的查询开始,例如

$sql = "SELECT SUM(delivery_details_revenue) as revenue, 
        FROM tbl_delivery_details
        WHERE tbl_customer_id_customer = '$id_customer'";

看看它是否返回任何东西。
如果是这样 - 开始添加更多数据以获取。
如果否 - 检查您的数据和整体数据结构是否正常。

于 2013-01-04T07:49:25.443 回答
0

如果我没看错,你就是在总结你的 while 循环之外的值。那是行不通的。

我认为你正在混淆一个正常的while循环和一个'do while'循环。

请参阅此代码:

      <?php do { ?>
          <tr>                          
              <td><?php echo $row_PK['delivery_details_revenue'];?></td>                                                       
      </tr>
      <?php } while ($row_PK = mysql_fetch_assoc($PK));?>
      <?php { ?>
        <?php  $sum+=$row_PK['delivery_details_revenue'] ?>
      <?php } ?>

它应该更多地沿着这些路线:

      <?php do { ?>
          <tr>                          
              <td><?php 
       echo $row_PK['delivery_details_revenue'];
       $sum+=$row_PK['delivery_details_revenue'] 
       ?>
      </td></tr>
      <?php } while ($row_PK = mysql_fetch_assoc($PK));?>

如果您将代码写得更清楚一点,就不会发生这种情况;尽量避免交错html和php这么多:

<?php 
  do { 
    $revenue = $row_PK['delivery_details_revenue'];
    $sum += revenue;
    println("<tr><td>$revenue</td></tr>");
  } while ($row_PK = mysql_fetch_assoc($PK));
?>

如果你问我,这要清楚得多。

于 2013-01-04T10:40:43.907 回答