0

Im trying to create a query that executes three sentence, but i think its not possible, the top its two, the issue is that i have two tables, one its called current_products and the other one its called finished_products, once a product is finished the data that is in the current_product table is passed to the finished_products table, that would be:

$query = self::connect()->prepare(
            "INSERT INTO `finished_products` 
                    (room, name, lot, quantity_packed, pallet) 
                SELECT room, name, lot, quantity_to_package, finished_pallets 
                FROM `current_products` 
                WHERE `room`=:room
            );
$query->execute(["room"=>$room]);

once its passed, i want to delete the data of the current_product table where the room is the same room that i pass, something like this:

$query = self::connect()->prepare(
            "INSERT INTO `finished_products` 
                    (room, name, lot, quantity_packed, pallet) 
                SELECT room, name, lot, quantity_to_package, finished_pallets 
                FROM `current_products` 
                WHERE `room`=:room; 
            DELETE * FROM `current_products` WHERE `room`=:room;"
        );
$query->execute(["room"=>$room]);

but like i said, it neither works nor possible, i was thinking to create a trigger that executes in the finished_products table once its execute the sentence INSERT in it, something like this:

CREATE TRIGGER DELETECURRENTPRODUCT_AI AFTER INSERT ON `finished_products` DELETE * FROM `current_products` WHERE room = new.room

but its always telling me that theres an error close to DELETE, so i dont know well how the syntax is, i tried a lot of ways but it seems doesnt work, other people told me to use transactions, but seeing it, its like execute two querys one after another one, like this:

$query = self::connect()->prepare(
            "INSERT INTO `finished_products` 
                (room, name, lot, quantity_packed, pallet) 
                SELECT room, name, lot, quantity_to_package, finished_pallets 
                FROM `current_products` 
                WHERE `room`=:room);

$query->execute(["room"=>$room]);

$query = self::connect()->prepare("DELETE * FROM `current_products` WHERE `room`=:room);
$query->execute(["room"=>$room]);

the difference its it will rollback if theres an error, but for what i trying to do i dont think i need to use transactions, is there a way to do it? thanks :)

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
plus
  • 327
  • 3
  • 12

0 Answers0