gpt4 book ai didi

php - 如何使用php从数据库中选择数据并对所选行的列求和并显示在页面中?

转载 作者:行者123 更新时间:2023-11-29 21:47:49 24 4
gpt4 key购买 nike

我正在尝试对从数据库中选择的行的列进行求和,并将结果显示在我网站的表格显示下。我正在使用 PHP 编码新手。请帮我。下面是我的代码:

提交按钮下方是一个表格,将在其中显示搜索结果。

 <form action="ViewAcc.php" method="get" >
Invoice/Check No.:<input type="text" name="rcchk">
<br>
OR
<br>
Retailer Name :<input type="text" name="rtsr">
<br>
<button class="button-special" value="Submit">SUBMIT</button>
</form>
<br>


<div class="9u 12u(mobile) important(mobile)">
<section align="left">
<table cellpadding="2px" cellspacing="2px" width="50%">
<tr><th>Invoice/Check No.</th><th>Retailer</th><th>Credit/Debit</th>
<th>Purpose</th></tr>
<?php
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['rcchk'] . "</td><td>" . $row['rtsr'] . "</td><td>"
. $row['crdbt'] . "</td><td>" . $row['purpose'] . "</td></tr>";
//$row['index'] the index here is a field name
}
?>
</table>
<table>
<tr><th></th><th>Total</th><th><?php echo $sum?></th><th></th></tr>
</table>

下面是我的 PHP 代码:

<?php
include ('dbase.php');

$rcchk = $_GET['rcchk'];
$rtsr= $_GET['rtsr'];

$query = "SELECT * , sum(crdbt) as total FROM account WHERE rcchk='$rcchk' OR rtsr='$rtsr'";
$result = mysql_query($query);

echo "<table>"; // start a table tag in the HTML

include('ViewAccount.php');
include('ViewAccount_2.php');
mysql_close();

?>

如何计算贷方/借方列的总和,特别是当我仅选择特定行(基于发票号码或零售商信息)时,以及我可以在哪里放置代码?请帮助我。

最佳答案

<?php
$conn = mysqli_connect("localhost","root","","dbname");
$query = "SELECT * FROM `tablename` WHERE WHERE rcchk='$rcchk' OR rtsr='$rtsr'";

$sql = mysqli_query($conn,$query);
$sum = 0;
//echo $sum before the loop and give it value 0.
while($row = mysqli_fetch_array($sql)){
$sum += $row['crdbt'];
}

echo $sum;
?>

关于php - 如何使用php从数据库中选择数据并对所选行的列求和并显示在页面中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33958447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com