Adding Values, how is it done?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitation
Forum Newbie
Posts: 10
Joined: Sat Aug 20, 2005 8:18 am

Adding Values, how is it done?

Post by nitation »

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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Adding Values, how is it done?

Post by jaoudestudios »

I am not totally sure what you mean, can you paste your code?

There are various ways: you could add another row and then when you want a total, sum up all the rows; or you can take the value in the field and add to it and then update that row with the new total value.

It depends on what you are using it for.
nitation
Forum Newbie
Posts: 10
Joined: Sat Aug 20, 2005 8:18 am

Re: Adding Values, how is it done?

Post by nitation »

Thank you for your reply.

I tot of using this query and it worked.
"UPDATE Table SET Amount = Amount + $newAmount";
regards
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Adding Values, how is it done?

Post by jaoudestudios »

it works?

do you still need help?

I would recommend not parsing the php variables, maybe use this instead:

$q = "UPDATE table SET amount = 'amout + ".$newamount."'";

are you sure it worked? you need to add a WHERE onto the mysql query, otherwise it does not know which entry to update, so it will do all the entries, or is that what you want it to do?
nitation
Forum Newbie
Posts: 10
Joined: Sat Aug 20, 2005 8:18 am

Re: Adding Values, how is it done?

Post by nitation »

I have included the where clause. Please tell why the dot(.) before and after the variable .$newamount.

Thanks in advance
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Adding Values, how is it done?

Post by jaoudestudios »

the dot (.) is to append/concat the php variable to the rest of the string.
Post Reply