NeedtoKnow Basis NeedtoKnow Basis - 1 year ago 50
SQL Question

HTML/PHP code to add numbers to a whole sum

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?

Iro Iro
Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download