4

I have a mysql database that has the column balance. This table is used for storing data about a user's account balance, so how to add numbers in mysql statement?

here is what I have so far

$sql_data = "UPDATE `database1`.`users` SET (`balance`) = '(what to put here?)'
WHERE ('" . $mysqlid . "') ";

so do I have to a) retrieve the current balance with a mysql select query? and b) what do I use to add the integer 5 to the column balance, the mysql row is double(16,2)

1
  • 2
    I feel like I should remind all of you to watch out for SQL injections, maybe by using prepared statements. Commented Jul 29, 2012 at 5:58

3 Answers 3

3
$sql_data = "UPDATE `database1`.`users` SET `balance` = MIN(`balance` + 5, (maximum_value_they_can_have)) WHERE ('" . $mysqlid . "') ";

Edit:

Arguably you can do a check to verify that balance + 5 doesn't go over the bounds of the balance data-type in PHP using something that is unlikely to run into issues, like bcmath functions. (Does math via given precision with string comparison)

Sign up to request clarification or add additional context in comments.

Comments

2

Try this :

$bal=100.5;  // balance to be add
$sql_data = "UPDATE `database1`.`users` SET `balance` = (`balance`+'$bal')  WHERE ('" . $mysqlid . "') ";

Comments

2

You can do this:

$sql_data = "UPDATE `database1`.`users` SET `balance` = (`balance` + [value to be added]) WHERE ('" . $mysqlid . "') ";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.