简体   繁体   中英

INSERT and Update at the same time or Use Transact in SQL Query

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:

  1. 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 | +----------+------------+----------------+-------------+------------+----------+----------+-----------+-----------+ 
  2. 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 | +---------+-----------------+------------+ 
  3. 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:

  1. i want to add an item into the bag item_id column w/ the chara's id
  2. then deducts that charas gold by the amount of the item he purchased(based on the shop item_price ).
  3. and then deduct the item_qty of the purchased item by 1 on the shop table.

can it be done in 1 query. i also want to use transact.

You cannot update multiple tables in one query. However, you can use transactions to make it happen at once:

$db = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$db->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

try {
    $db->beginTransaction();

    $q1 = $db->prepare("INSERT INTO bag(bag_id, chara_id, item_id, item_qty)VALUES(NULL,:id,:item_id,'1')");
    $q1->bindValue(':id', $yourId);
    $q1->bindValue(':item_id', $yourItemId);
    $q1->execute();

    $q2 = $db->prepare("UPDATE chara SET chara_gold = chara_gold - :gold WHERE chara_id = :id");
    $q2->bindValue(':gold', $yourItemPrice);
    $q2->bindValue(':id', $yourCharaId);
    $q2->execute();

    $q3 = $db->prepare("UPDATE shop SET item_qty = :qty WHERE item_id = :item_id");
    $q3->bindValue(':qty', $yourQty);
    $q3->bindValue(':item_id', $yourItemId);
    $q3->execute();

    $db->commit();
} catch (Exception $e) {
    $db->rollback();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM