Unable to sum certain values

You have a space before the semicolon on the TotalPrice line. Also, you have TWO input fields with the same name. The TotalPrice name should be name=“total” or something other than price!

i cant find that where it is

*** Note the *** Change the second price and remove the extra space…

<?php
$grand_total = 0;
while ($resul=mysqli_fetch_array($quer)) {
$grand_total = $grand_total + $resul["TotalPrice"]; // 54 line
?>
		<tr>
			<td><input type="date" name="date" placeholder="yyyy/mm/dd" value="<?php echo $resul['date'];?>"></td>
			<td><input type="text" name="name" value="<?php echo $resul['name'];?>"></td>
			<td><input type="text" name="amount"value="<?php echo $resul['amount'];?>"></td>	
			<td><input type="text" name="count" value="<?php echo $resul['count'];?>"></td>
			<td><input type="text" name="price" value="<?php echo $resul['price'];?>"></td>
			<td><input type="text" name="total" value="<?php echo $resul['TotalPrice'];?>"></td>
			<td><a href="deleteFIN2.php?name=<?php echo $resul["name"]; ?>">Delete</a></td>
	</tr>
	<?php
	}
        //  Display grand total line
         echo "<tr><td colspan='5'></td><td>Grand Total:</td><td>" . $grand_total . "</td></tr>";
	?>
still Notice: Undefined index: TotalPrice in C:\wamp64\www\forma\refinc.php on line 54

Did you change the query to include the new TotalPrice SUM function?

{
		$from_date = $_POST['from_date'];
        $to_date = $_POST['to_date'];
        $sql="SELECT date, income.name, income.amount, count, menu.price, SUM(income.count*menu.price) as TotalPrice FROM income JOIN menu ON menu.name = income.name AND menu.amount = income.amount  WHERE date BETWEEN '$from_date' AND '$to_date'"; 
		$quer=mysqli_query($conn,$sql) ;
		
}
yes

What does your server logs say?

Are you using a local software server like Wamp or Xamp or a real server somewhere?
If Wamp or Xamp, look in your C drive under Wamp32 or Wamp64 and you will find a LOGS folder.
In there are all your errors. Delete them all. Run your page. Look back at the folder and refresh it.
Then, look at the PHP error log. It should be text so double-click on it to open and it should tell you all about the error that is making your code fail. I do see on extra space on the mysqli_query line, don’t think that is a problem.

I have to leave shortly for many hours. You could send me the entire file in a private message and I can look thru it when I get back. I will be gone for about 8 hours… I think you are very close to solving all of this!

this is the error the repеat
“[25-Jun-2021 18:58:37 UTC] PHP Notice: Undefined index: TotalPrice in C:\wamp64\www\forma\refinc.php on line 54
[25-Jun-2021 18:58:37 UTC] PHP Stack trace:
[25-Jun-2021 18:58:37 UTC] PHP 1. {main}() C:\wamp64\www\forma\refinc.php:0
[25-Jun-2021 18:58:37 UTC] PHP Notice: Undefined index: TotalPrice in C:\wamp64\www\forma\refinc.php on line 62
[25-Jun-2021 18:58:37 UTC] PHP Stack trace:
[25-Jun-2021 18:58:37 UTC] PHP 1. {main}() C:\wamp64\www\forma\refinc.php:0”

In the query, you SELECT " count ", but, in the SUM, you are using " income.count ".
I think that is the problem. make count into income.count. I am guessing, but, something is not correct there!

You cannot use an sql aggerate function, such as SUM(), this way and get both the data and the SUM() in one query. You will only get one row of data in the result. You can just multiply the income.count * menu.price and get the TotalPrice per row (which is moving this multiplication from the php code to the sql statement.) You still need to add up the individual TotalPrice values in the php code to arrive at the grand total.

Next, Don’t Repeat Yourself (DRY) and Keep It Simple Stupid (KISS.) The common part of the data retrieval query should only exist once. Your current issue with the undefined index TotalPrice is because you don’t have that in each of the queries. The only difference between the (three) data retrieval queries is (should be) the WHERE clause. You should only have conditional logic for things that are different. Start with a php variable that contains the common part of the data retrieval query. Your conditional logic that tests for the name input or the date inputs should only build and append the appropriate WHERE clause to the common part of the query. You then execute and fetch the data from this single query.

PHDR, he WANTS to get the sum of each row. So he can display it on each line.

still the same error Notice: Undefined index: TotalPrice in C:\wamp64\www\forma\refinc.php on line 54

Yes, but SUM() is an aggerate function. It operates on GROUP’ed BY data or if there is none in a query, it operates on all the rows in the result set. To get a per row total, you just multiple the two column values in the row.

Is this what you are using for the query? This should work. I have to leave for awhile. Send the file to me in a private message and I will test it tonight when I get back. It should work.

The code is correct. But, you need to remove all SUM .

       $sql="SELECT date, income.name, income.amount, count, menu.price, income.count*menu.price as totalprice FROM income JOIN menu ON menu.name = income.name AND menu.amount = income.amount  "; 

Also make rename totalprice not TotalPrice.

Sponsor our Newsletter | Privacy Policy | Terms of Service