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