2016-12-29 10 views
0

残りの列と同じ行に結果を表示しますが、すべての行で同じ結果(最初の行の合計)が得られます。PHPによるSQLデータベースの総和

<?php 
    $sub = mysql_connect ("localhost", "root", ""); 
    if (!$sub) 
    { 
    die('Could Not Connect:' .mysql_error()); 
    } 
    mysql_select_db("tabsc",$sub); 
    if (!empty($_POST)) 
    { 
    $sql= "INSERT INTO fund (dat, sour, condi, amount, disburse, othe, expenditure) 
    VALUES ('$_POST[dat]','$_POST[sour]','$_POST[condi]','$_POST[amount]','$_POST[disburse]','$_POST[othe]','$_POST[expenditure]')"; 
    header('location: funding.php'); 
    if (!mysql_query($sql, $sub)) 
    { 
    die('Error' .mysql_error());}}  

    //start add sql column value on the top 
    $result = mysql_query('SELECT SUM(amount) AS amount FROM fund'); 
    $row = mysql_fetch_assoc($result); 
    $sum = $row['amount']; 

    $result = mysql_query('SELECT SUM(disburse) AS disburse FROM fund'); 
    $row = mysql_fetch_assoc($result); 
    $sumd = $row['disburse']; 

    $result = mysql_query('SELECT SUM(othe) AS othe FROM fund'); 
    $row = mysql_fetch_assoc($result); 
    $sumo = $row['othe']; 

    $result = mysql_query('SELECT SUM(expenditure) AS expenditure FROM fund'); 
    $row = mysql_fetch_assoc($result); 
    $sume = $row['expenditure']; 
    $remh=$sum-($sumd+$sumo+$sume); 
//end add sql column value on the top 

    $resul=mysql_query("SELECT * FROM fund"); 
    echo "<table border='1'> 
     <tr> 
     <th></th> 
     <th></th> 
     <th>Total</th> 
     <th>$sum</th> 
     <th>$sumd</th> 
     <th>$sumo</th> 
     <th>$sume</th> 
     <th>$remh</th> 
     <th></th> 
     <tr> 
     <th>Date</th> 
     <th>Source</th> 
     <th>Condition</th> 
     <th>Amount</th> 
     <th>Disburse</th> 
     <th>Other Payament</th> 
     <th>Expenditure</th> 
     <th>Remaining</th> 
     <th>Delete/Edit</th> 
     </tr>"; 

//Row Wise Sum (I think problem is here) 
$result =mysql_query('SELECT id, SUM(amount + disburse + othe + expenditure) AS remaining FROM fund GROUP BY id'); 
$row = mysql_fetch_assoc($result); 
$sumrem = $row['remaining']; 



    while ($row = mysql_fetch_array($resul)) 
    { 
    echo '<tr>'; 
    echo '<td>' .$row['dat'].'</td>'; 
    echo '<td>' .$row['sour'].'</td>'; 
    echo '<td>' .$row['condi'].'</td>'; 
    echo '<td>' .$row['amount'].'</td>'; 
    echo '<td>' .$row['disburse'].'</td>'; 
    echo '<td>' .$row['othe'].'</td>'; 
    echo '<td>' .$row['expenditure'].'</td>'; 
    echo "<td>$sumrem</td>"; //Result will be into this columne 
    echo '<td><a href="delete.php? id='.$row['id'].'">Del</a> || <a href="edit.php? id='.$row['id'].'">Edit</a>'; 
    echo '</tr>'; 
    } 
    echo '</table>'; 
    mysql_close($sub); 
    ?> 
+0

*「すべての行で同じ結果(最初の行の合計)が得られました」* - 結果はどのようなものになりましたか、目的の結果は何ですか?このためにあなたのスキーマと値を掲示し、おそらくこれのためにフォームを設定してください。 –

+0

DBの最初の行の合計が得られます。残りの列の各行の合計が望ましい結果になります。ありがとう – sadi

+0

*** [mysql_ *関数の使用をやめてください](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)*** [これらextensions](http://php.net/manual/en/migration70.removed-exts-sapis.php)がPHP 7で削除されました。[prepared](http://en.wikipedia.org/wiki/)について学んでください。 [PDO](http://php.net/manual/en/pdo.prepared-statements.php)と[MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared)のステートメント(Prepared_statement) -statements.php)、PDOの使用を検討してください。[これは本当に簡単です](http://jayblanchard.net/demystifying_php_pdo.html)。 –

答えて

0
  • 私はあなたが得る にIDを渡すために、ループ内のそのクエリを実行する必要があると思うそうでなければ、特定の 行は、ID順に依存しないのか分からないだろう、行ごとの残り。
  • データベースへの呼び出し回数を最小限に抑えようとすると、私は個人的にすべてを選択し、PHP式を使って総計を得て、残りの行をその呼び出しで取得します。
  • 総計を取得するなどのように、別々のトランザクションでそうしたように多くの呼び出しを行うと、合計を取得している間にユーザーが金額を変更して間違った金額を表示するリスクが発生します。したがって、すべての列と残りの計算された列を取得し、合計額を計算します。
0

最初の行だけでなくすべての行の合計を取得する場合は、GROUP BYを使用しないでください。

$result =mysql_query('SELECT SUM(amount + disburse + othe + expenditure) AS remaining FROM fund'); 
$row = mysql_fetch_assoc($result); 
$sumrem = $row['remaining']; 

また、1つのクエリに他のすべてのSELECT SUM()のクエリを組み合わせることができます。

$result = mysql_query('SELECT SUM(amount) AS amount, SUM(disburse) AS disburse, SUM(othe) as othe, SUM(expenditure) AS expenditure FROM fund'); 
$row = mysql_fetch_assoc($result); 
$sum = $row['amount']; 
$sumd = $row['disburse']; 
$sumo = $row['othe']; 
$sume = $row['expenditure']; 
$remh = $sum - $sumd - $sumo - $sume; 
$sumrem = $sum + $sumd + $sumo + $sume; 
+0

残りの列とすべての行に列の量、出金、その他、支出の合計が同じ結果を示しています。私は同じ行の残りの列に行の値を追加しようとしている、私は自動インクリメントで一意のID列を持っています。 – sadi

+0

質問から何をしようとしているのか分からず、コードから推測しました。あなたは残りの列で稼いでいる合計をしようとしていますか? – Barmar

0

おかげで一人一人... を私はアイデア&その作業罰金を得ました。 はDBにデータを入力した後、私は、残りの列に結果をエコーテーブル

$sql =mysql_query('UPDATE fund SET remaining = (amount - (disburse + othe + expenditure))'); 

でこのクエリを実行します。