You can try this partner:
DROP TRIGGER IF EXISTS trg_product_total; DELIMITER // CREATE TRIGGER trg_product_total AFTER INSERT ON product FOR EACH ROW BEGIN SET @price = NULL, @quantity = NULL; SELECT price INTO @price FROM product WHERE id = NEW.id; SELECT quantity INTO @quantity WHERE id = NEW.id; UPDATE product SET total = @price * @quantity WHERE id = NEW.id; END;
This approach can be used if you really do not want to process the product. Before embedding it in the database.
The trigger will be executed every time a new record is added to the table, where the expected insertion for the common column will be either "NULL" or "0" depending on your default value.
But I think it would be better if you calculated it before inserting.
The stream will look like this:
Application side 1. get price and quantity for the product 2. calculate for the total 3. insert values into the query 4. execute query
If you want to know more about MySQL Trigger: Link
Also, PHP Transaction: Link
source share