Nama Nama - 1 month ago 8
MySQL Question

Why is math being saved to MySQL database?

I have a table with a column populated by a formula:

if ($row['importfee'] > 0) {
$sum_total= $row['stock']*($row['cost']*($row['importfee']/100)+$row['cost']+$row['prepcost']);
print ($sum_total);
$total_price += $sum_total;
}else{
$sum_total= $row['stock']*($row['cost']+$row['prepcost']);
echo $sum_total;
$total_price += $sum_total;
}


ex. stock is 400, cost is 200, prepcost is 0 - output is 80000
This column is displayed as plain text, not an input.

Next I send $sum_total to a hidden input for $_POSTing:

<input name='totalcost[]' type='text' id='totalcost' value='<?php echo $sum_total ?>'>


ex. output is 80000

Finally I update my database:

$totalcost= $_POST['totalcost'];

$sql = "UPDATE $tbl_name2 SET
productname='".$productname[$i]."',stock='".$stock[$i]."',cost='".$cost[$i]."',maker='".$maker[$i]."',customer='".$customer[$i]."',department='".$department[$i]."',comment='".$comment[$i]."',reportdate='".$reportdate."',page='".$page[$i]."',importfee='".$importfee[$i]."',prepcost='".$prepcost[$i]."',totalcost='".$totalcost[$i]."*(".$cost[$i]."+".$prepcost[$i].");'
WHERE id='".$id2[$i]."'";


And I get a wonderful surprise in my database:
ex. database entry [totalcost] 80000*(200+);

I expected the entry to be [totalcost] 80000.

Why is the formula being sent to the database but not echoed in my table? How can I send just the value 80000 without the math to the database? I'm working on a WAMP server at the moment if t makes any difference.

Answer

Added as answer (from comment):

Your code is working exactly as it should - here's what you put into the [totalcost] column:

totalcost='".$totalcost[$i]."*(".$cost[$i]."+".$prepcost[$i]‌​.");

All of that goes into the column.