user756659
user756659

Reputation: 3512

mysql - update value is current value plus new value

I have the following update query. shots_limit needs to be its current value in the database PLUS my new value $add_shots. Is there any way to do this when using bindings?

//figure out how many screenshots to add
$add_shots = $_POST['Quantity'] * 500;

$stmt = $db->prepare("
    UPDATE accounts 
    SET orders = :orders,
        shots_limit = :shots_limit
    WHERE account_id = :account_id  
");

//bindings
$binding = array(
    'orders' => $orders_str,
    'shots_limit' => $add_shots + ORIGINAL VALUE
    'account_id' => $result['account_id']
);

$status = $stmt->execute($binding);

Upvotes: 2

Views: 2297

Answers (1)

Stepashka
Stepashka

Reputation: 2698

I assume this is what you are looking for. Just use current value in update

$stmt = $db->prepare("
    UPDATE accounts 
    SET orders = :orders,
        shots_limit = shots_limit + :shots_limit
    WHERE account_id = :account_id  
");

//bindings
$binding = array(
    'orders' => $orders_str,
    'shots_limit' => $add_shots,
    'account_id' => $result['account_id']
);

Upvotes: 2

Related Questions