我希望能够总结页面中显示的所有收入,并且每次我将其他数据添加到收入列时它会自动汇总:
以下是我的代码:
<?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始终为零如何正确地总结我所做的列,如果我向其添加另一个数据,它将自动求和:
答案 0 :(得分:2)
为什么不让MySQL在查询中为你做这个呢?
而不是在PHP中添加收益值$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'];
答案 1 :(得分: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'";
并查看是否返回任何内容 如果是这样 - 开始添加更多数据来获取 如果不是 - 检查您的数据和整体数据结构是否正常。
答案 2 :(得分: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));
?>
如果你问我,这会更清楚。