nitation Posted July 1, 2008 Share Posted July 1, 2008 Hello people, I can insert a value for a field named amount in my database, what i wanna do now is to add another value and it sum it up. My question is how do i go about it? do i insert a new value in the amount field on another row and add it? if yes, how do i do that in php. Thanks in advance Link to comment Share on other sites More sharing options...
lemmin Posted July 1, 2008 Share Posted July 1, 2008 This SQL should work. "UPDATE Table SET Amount = Amount + $newAmount"; Link to comment Share on other sites More sharing options...
nitation Posted July 1, 2008 Author Share Posted July 1, 2008 Thank you Lemmin. It worked Link to comment Share on other sites More sharing options...
nitation Posted July 1, 2008 Author Share Posted July 1, 2008 @lemmin The code you gave me worked fine. But the question is if i wanna remove the value i added new, how would it reverse to the first value entered. Thanks in advance Link to comment Share on other sites More sharing options...
nitation Posted July 1, 2008 Author Share Posted July 1, 2008 Guys i hope my question is clear Link to comment Share on other sites More sharing options...
lemmin Posted July 1, 2008 Share Posted July 1, 2008 There wouldn't be a way to reverse it without knowing how much you added. If you keep the value somewhere, the same code would work with subtraction: "UPDATE Table SET Amount = Amount - $newAmount"; Link to comment Share on other sites More sharing options...
nitation Posted July 2, 2008 Author Share Posted July 2, 2008 @lemmin You said if i keep the code somewhere. How can that be done. Do i need to insert each value for amount on a new row and sum it up or what. Please give me a rough idea on how to carry it out. Thanks in advance Link to comment Share on other sites More sharing options...
lemmin Posted July 2, 2008 Share Posted July 2, 2008 It depends on how long you need to keep the old value. If it can only be changed while the user is logged in, but not after he/she has logged out once, then you can save the number in a session. If the value may be changed at a later time, the best way would probably be to keep the value in your database, like you said. Link to comment Share on other sites More sharing options...
nitation Posted July 3, 2008 Author Share Posted July 3, 2008 @lemmin, Thank you for your reply. The question now is; if am trying to insert an amount value for a user using an HTML form that will be stored in a database. Do i need to create another HTML form to add another value before they can be summed up. The reason am asking this is because, if i will be using the same HTML form to add the 1st and second value, how do i query the database to do the addition. I hope am clearer Link to comment Share on other sites More sharing options...
Recommended Posts