0

I have a table in mysql with some variable:

data price 1 price 2 price 3 price 4

I have a form in a html page with the same field (data;price 1;price 2;price 3;price 4) that client must select (they are option value form)

I use this php formula to calculate an estimate

$query = "SELECT SUM(price1)FROM preventivo Where data>='$_POST[arrivo]' and data <'$_POST[partenza]'"; 
$dave= mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($dave)){ 
foreach($row as $cname => $cvalue){ 
print "$cvalue\t";}
}

If I repeat this formula for all price (price 1,price2, price3 and price 4) the form doesn't work

Finally I need a fomula that takes into account the possible value of all 4 prices and do the sum among them their

3
  • 1
    If your form's elements contains a space price 1 etc. then that is one reason for it to fail. Use undercores price_1. Plus, if your table's columns also contain spaces, then either wrap them in backticks, or use underscores for those also. It's hard to say for sure, without seeing the actual codes used. Commented Feb 6, 2014 at 18:32
  • no in the table is price1...I fall to write Commented Feb 6, 2014 at 18:54
  • Just checking. Some actually use what they've posted in their questions for actuals. Commented Feb 6, 2014 at 18:55

1 Answer 1

1

You need to assign aliases to your sums:

$query = "SELECT SUM(price1) AS sum1, SUM(price2) AS sum2, SUM(price3) AS sum3, SUM(price4) AS sum4
          FROM preventivo Where data>='$_POST[arrivo]' and data <'$_POST[partenza]'";

Then you can access them as $row['sum1'], $row['sum2'] and so on.

P.S. You should convert from the deprecated mysql_XXX functions to PDO or mysqli, and use parametrized queries.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry so I need to write $query = "as you said"; $dave= mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($dave)){ foreach($row['sum1'] as $cname => $cvalue){ print "$cvalue\t"; } } ($row['sum12'] as $cname => $cvalue){ print "$cvalue\t"; } } etc??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.