2013-02-20 86 views
0

我有一個錶鏈接,但通過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

回答

0

這就是問題所在:

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

需求是:

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

,並聽取來自@ h2ooooooo的建議 - 去查找更好更安全的使用PHP/MySQL的方法。