当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Subtracting two totals

问题描述:

I have a table linked though mysql. I have 5 columns showing: Name, Shop, Description, Amount Owed and cost. I have written a script to tally up the totals of of "amount owed" and "cost". I am having trouble with subtracting the total "cost" from the total "Amount Owed".

Here is my code below.

<div>

<table id="datatables" class="display">

<thead>

<tr>

<th>Name</th>

<th>Shop</th>

<th>Description</th>

<th>Amount due</th>

<th>Cost</th>

</tr>

</thead>

<tbody>

<?php

while ($row = mysql_fetch_array($result)) {

?>

<tr>

<td><?=$row['name']?></td>

<td><?=$row['category']?></td>

<td><?=$row['subject']?></td>

<td><?=$row['custom1']?></td>

<td><?=$row['custom2']?></td>

</tr>

<?php

}

?>

<?php

$query = "SELECT custom1, SUM(custom1) FROM hesk_tickets";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

echo "Total Owed". $row['custom1']. " = £". $row['SUM(custom1)'];

echo "<br />";

}

?>

<?php

$query = "SELECT custom2, SUM(custom2) FROM hesk_tickets";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

echo "Cost exVAT". $row['custom2']. " = £". $row['SUM(custom2)'];

echo "<br />";

echo "Profit". $row['custom1 , custom2']. " = £". $row['SUM(custom1 - custom2)'];

echo "<br />";

}

?>

</tbody>

</table>

</div>

Any help will be greatly appreciated

网友答案:

This is the problem:

echo "Profit". $row['custom1 , custom2']. " = £". $row['SUM(custom1 - custom2)'];

needs to be:

echo "Profit". $row['custom1']. ",".$row['custom2']." = £". $row['custom1'] - $row['custom2'];

and heed the advice from @h2ooooooo - go and look up better and more secure ways of using PHP/MySQL.

分享给朋友:
您可能感兴趣的文章:
随机阅读: