简体   繁体   中英

How to select data from database and sum the column of the row selected also display in page using php?

I am trying to sum up the column of the row selected from the database and displays the result under the table display in my website. I am using new to PHP coding. Please help me. Below is my code:

Below the submit button, is a table where it will display the result of the search.

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

Below here is my PHP code:

<?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();

?>

How can I calculate the sum of the Credit/Debit column especially when I have selected only specific rows(based on the Invoice No. or Retailer Info) and where can I place the codes? Please help me.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM