How to simplify this database update request (php)

How can I simplify this update instruction? Whenever a customer buys an item, it updates sales with 1.

$this->db->query('SELECT amount FROM shop_items WHERE itemid='.$itemid.'');   
$new_amount = $item->amount+1;
    if(!$this->db->query('UPDATE shop_items SET amount='.$new_amount.' WHERE itemid='.$itemid.'')):
    return false;
    endif;

Could I make it simpler, so there are fewer lines, for example:

if(!$this->db->query('UPDATE shop_items SET amount=_current_value_+1 WHERE itemid='.$itemid.'')):
return false;
endif;

Is it possible? if not thank you at all

+3
source share
1 answer

How to use the following SQL query:

update shop_items
set amount = amount + 1
where itemid = 123

Basically, in only one SQL query do you set amountto the previous value that it had, plus one.
No need for two queries; -)


Integrating this into your PHP code should give you something similar to this:

if (!$this->db->query('UPDATE shop_items SET amount=amount+1 WHERE itemid='.$itemid.'')) :
    return false;
endif;

. _current_value_ , : amount.

+6

Source: https://habr.com/ru/post/1742581/


All Articles