Alae
Alae

Reputation: 11

PDO: How to add a value to an existing value for multiple columns using UPDATE

I have a table "api_keys" where i am storing some API Keys.

The api_keys Table has 5 columns: user, key, api1_limit, api2_limit, api3_limit.

I will be using the last 3 columns to store the sum of each API request. i want to do this because there is an API Limit, so i want to make sure to not pass the limit.

So the idea is to add values to "api limits" columns each time i use the API.

By the way, i am using PDO.

I tried to use the default UPDATE syntax to add to values to the existing values.

The issue is that the 'api1_limit' is the only column that get updated.

$sql_query = "UPDATE api_keys 
            SET api1_limit = api1_limit + :api1_limit_counter,  api2_limit =  api2_limit + :api2_limit_Counter, api3_limit =  api3_limit + :api3_limit_Counter WHERE key = :thekey";


$statement = $connection->prepare($sql_query);
$result = $statement->execute(
    array(
        ':api1_limit_counter'      =>  $api1_limit_counter,
        ':api2_limit_Counter'       =>   $api2_limit_Counter,
        ':api3_limit_Counter'       =>   $api3_limit_Counter,
        ':thekey'                  => $thekey

         )
);

I want to update values in all the 3 columns using 1 call.

Upvotes: 0

Views: 53

Answers (1)

Alae
Alae

Reputation: 11

I found what was the issue, the "api limits" columns are set to NULL. you can not add to the value if the initial value is NULL, so i start setting the values to 0 when i am adding credentials to the api_keys table, now everything works fine.

Upvotes: 1

Related Questions