Bu veritabanı güncelleme sorgusu basitleştirmek nasıl (php)

1 Cevap php

Nasıl bu güncelleştirme talimat basitleştirmek olabilir? Bir istemci bir öğe satın zaman o 1 ile satış güncelleyecektir.

$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;

Ben daha basit yapamazsınız böylece daha az hatları, örneğin vardır:

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

Bu mümkün mü? değil teşekkürler zaten eğer

1 Cevap

Ne Aşağıdaki SQL sorgusu kullanma hakkında:

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

Basically, in just one SQL query, you set the amount to the previous value it had, plus one.
No need for two queries ;-)


Integrating this in your PHP code should give you something that looks like this :

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

Not: Ben sadece üzerinde çalıştığınız alanın adı ile _current_value_ değiştirilmiştir: amount.