NathaliaZeed
NathaliaZeed

Reputation: 149

MYSQL INT addition

I'm trying to bring the INT values from mysql and do a addition and finally update the database. But this doesn't seem to do update? How can I fix this?

$resultSecond = mysql_query("SELECT * FROM Furniture");
while($lineSecond = mysql_fetch_array($resultSecond)) {
    $item     = $lineSecond["Item"]; 
    $janD     = $lineSecond["January"];
    $febD     = $lineSecond["February"];
    $marD     = $lineSecond["March"];
    $pastVals = $lineSecond["yearSale"];
    $totalCT  = $lineSecond["monthSale"];

    $totThisM = ($janD + $febD + $marD + $pastVals + $totalCT);
    mysql_query("UPDATE Furniture SET Furniture = '".$totThisM."' WHERE Item ='$item' LIMIT 1");
}

Upvotes: 0

Views: 59

Answers (2)

ʰᵈˑ
ʰᵈˑ

Reputation: 11375

Why not just use MySQL?

UPDATE `Furniture`
SET `Furniture` = (`January`+`February`+`March`+`yearSale`+`monthSale`) 

Test data

+---------+---------+-------+
| number1 | number2 | total |
+---------+---------+-------+
|       1 |       5 |     0 |
|       2 |       3 |     0 |
+---------+---------+-------+

And I run my query;

UPDATE table SET total = (`number1`+`number2`);

And the table is updated;

+---------+---------+-------+
| number1 | number2 | total |
+---------+---------+-------+
|       1 |       5 |     6 |
|       2 |       3 |     5 |
+---------+---------+-------+

Notes

  • Removed WHERE clause after comment from CodeBird.
  • Adjusted query after comments from Ravinder

Upvotes: 2

ncrocfer
ncrocfer

Reputation: 2560

Try this :

mysql_query("UPDATE Furniture SET Furniture = '". $totThisM ."' WHERE Item ='". $item ."' LIMIT 1");

Edit

Not the right answer, see comments for details.

Upvotes: 0

Related Questions