-2

I am using php 7.2...

The code is working fine

<?php 

    $add=mysqli_query($conn,'SELECT ac_id, SUM(amount)  from stac where ac_id="117" '); 
    while($row1=mysqli_fetch_array($add)) { $mark=$row1['SUM(amount)'];  
    echo $mark ; }?>

but the code below is not working. Please help me.

<?php 
  $ac_id=$_POST['ac_id'];


    $add=mysqli_query($conn,'SELECT ac_id, SUM(amount)  from stac where ac_id=$ac_id '); 
    while($row1=mysqli_fetch_array($add)) { $mark=$row1['SUM(amount)'];  
    echo $mark ; }?>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Shyful74
  • 29
  • 7

3 Answers3

0

Its seems like your id field is not integer so you should pass variable into single quote (') and also pass your query in double quote (") so variable will parse correctly.

Replace your query with this

$add=mysqli_query($conn,"SELECT ac_id, SUM(amount)  from stac where ac_id='$ac_id' ");
shubham715
  • 3,324
  • 1
  • 17
  • 27
0

Did you forget to add the group by when using an aggregate? Like so:

 SELECT ac_id, SUM(amount)  from stac where ac_id=$ac_id group by ac_id
Claudiu Haidu
  • 817
  • 4
  • 12
  • 24
-1

Try changing your query to

$add=mysqli_query($conn,"SELECT ac_id, SUM(amount)  from stac where ac_id='$ac_id' ");
Maaz Ali
  • 83
  • 5