i have these 3 queries:
$sql1 = "INSERT INTO bag(bag_id, chara_id, item_id, item_qty)VALUES(NULL,:id,:item_id,'1')";
$sql2 = "UPDATE chara SET chara_gold = chara_gold - :gold WHERE chara_id = :id";
$sql3 = "UPDATE shop SET item_qty = :qty WHERE item_id = :item_id";
and have this 3 tables:
chara
+----------+------------+----------------+-------------+------------+----------+----------+-----------+-----------+ | chara_id | chara_name | chara_class_id | chara_level | chara_gold | chara_hp | chara_mp | chara_atk | chara_def | +----------+------------+----------------+-------------+------------+----------+----------+-----------+-----------+ | 1 | LawrenceX | 1 | 1 | 1000 | 1000 | 1000 | 7 | 3 | | 3 | Viscocent | 2 | 1 | 1000 | 900 | 1100 | 5 | 5 | | 4 | Piatos | 1 | 1 | 1000 | 1000 | 1000 | 7 | 3 | | 5 | Hello | 1 | 1 | 1000 | 1000 | 1000 | 2 | 8 | | 6 | Sample | 3 | 1 | 1000 | 1100 | 900 | 9 | 1 | | 8 | Sampuro | 2 | 1 | 1000 | 1500 | 100 | 5 | 5 | | 12 | fail | 2 | 1 | 1000 | 900 | 100 | 5 | 5 | +----------+------------+----------------+-------------+------------+----------+----------+-----------+-----------+
bag
+---------+-----------------+------------+ | item_id | item_name | item_price | +---------+-----------------+------------+ | 0 | Halberd | 400 | | 1 | Axe | 200 | | 2 | Wooden Sword | 225 | | 3 | Dagger | 55 | | 4 | Bow | 120 | | 5 | Helmet | 155 | | 6 | Tunic | 50 | | 7 | Armour | 150 | | 8 | Necklace | 199 | | 9 | Studded Leather | 240 | +---------+-----------------+------------+
shop
+---------+---------+------------+----------+ | shop_id | item_id | item_price | item_qty | +---------+---------+------------+----------+ | 1 | 1 | 200 | 99 | | 2 | 2 | 225 | 99 | | 3 | 3 | 55 | 99 | | 4 | 4 | 120 | 99 | | 5 | 5 | 155 | 99 | | 6 | 6 | 50 | 99 | | 7 | 7 | 150 | 99 | | 8 | 8 | 199 | 99 | +---------+---------+------------+----------+
the scenario:
- i want to add an item into the bag
item_id
column w/ thechara's id
- then deducts that
charas gold
by the amount of the item he purchased(based on the shopitem_price
). - and then deduct the
item_qty
of the purchased item by 1 on theshop
table.
can it be done in 1 query. i also want to use transact.