1

Right now the value from the table is 1 and I want to add 5 so the final result would be 6.

The update always gets 5 so how can i just add 5 to the current number from the table?

$amount = 5

$sql = "UPDATE `table_projects` SET `package1`= $amount WHERE `id`='$userid'";
DigiNet Events
  • 357
  • 2
  • 14

1 Answers1

2

You will simply need to add package column on the right side of the operator = to add the $amount to itself.

$sql = "UPDATE `table_projects` 
        SET `package1`= `package1` + " . (int)$amount . " 
        WHERE `id` = '$userid'";

Notes:

  • I have added explicit typecasting to (int) on $amount. If it is going to be decimal/float value, you can use (float) instead.
  • Your code is open to SQL injection related attacks. Even real_escape_string cannot secure it completely. Please learn to use Prepared Statements
  • If you are not going to use Prepared Statements, and if the id ($user_id) is integer value.

I would do typecasting on it as well

$sql = "UPDATE `table_projects` 
        SET `package1`= `package1` + " . (int)$amount . " 
        WHERE `id` = '" . (int)$userid . "'";
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57