0

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?

2 Answers2

1

To change balance for specific user your mysql query must be like:

UPDATE users SET balance = balance + 50 WHERE id = 1
f0rtis
  • 326
  • 2
  • 13
1

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.

Community
  • 1
  • 1
Iro
  • 36
  • 3