减去两个总计

问题描述:

我有一个表链接,但通过MySQL。我有5栏显示:名称,商店,说明,金额和成本。我写了一个脚本来统计“欠款额”和“成本”的总和。我从总“欠金额”减去总“成本”时遇到问题。减去两个总计

这是我的代码如下。

<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> 

任何帮助,将不胜感激

+2

[**请不要在新代码中使用'mysql_ *'函数**](http://bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 – h2ooooooo 2013-02-20 14:28:50

+0

'$ row ['custom1,custom2']'应该是什么?虽然它不是无效的语法,但从查询结果中获取数据是完全错误的方法。 – 2013-02-20 14:29:31

这就是问题所在:

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

需求是:

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

,并听取来自@ h2ooooooo的建议 - 去查找更好更安全的使用PHP/MySQL的方法。