0

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");
}
3
  • use mysql_error() and see whats going on for the update command. Commented Feb 12, 2014 at 9:46
  • We don't know, because you don't show either the database schema or the error message you get. But we do know that this is a bad way to update; you can update all your database with a single UPDATE query, as in UPDATE Furniture SET Furniture = January + February + .... Commented Feb 12, 2014 at 9:46
  • Why there is a "LIMIT" in your update statement ??? Commented Feb 12, 2014 at 9:46

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

yep, you don't even need the WHERE Item=$item neither the while loop
You also don't need item field value for summing up.
It should be just SET Furniture = ( January + February + March + yearSale + monthSale )
Thanks h | CodeBird | Ravinder.
0

Try this :

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

Edit

Not the right answer, see comments for details.

3 Comments

won't change anything
I think the variable is not converted into its value. In PHP you can pass variable into double quotes (like $return = "The value is $var";) but not $return = 'The value is $var'; // Error).
it is in double quotes, the whole query is in double quotes. That's not his issue. He has " then ' so the var will work normally there.

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.