Reputation: 1
Lets just say I am keeping track of money. So I have my balance in a mysql database. My balance would be $100
I want to create a html script so when I enter say 50 and hit submit it will change the database value to 150.
I know the basics of how to connect to a mysql and insert and get data from a database, thats no problem I done that many times but instead of hitting submit and having the value change to just 50 how can I code it so it just adds on to the value so it will be 150?
Upvotes: 0
Views: 917
Reputation: 36
Assuming $value
contains the number you want to add to the amount.
$sql = "UPDATE table_name SET current_balance = current_balance + $value`;
Running the query will add $value
to whatever you had as current_balance
. You can read up on prepared statements, SQL injection etc to make the query I've written more secure.
Upvotes: 1
Reputation: 326
To change balance for specific user your mysql query must be like:
UPDATE users SET balance = balance + 50 WHERE id = 1
Upvotes: 1