Question

I'm Trying to do something like this to import pricing and stock from my providers. Its not working always.

  • When i do it from my phpMyAdmin works fine, the trigger is launched and the data imported fine.
  • When i do it from a php script witch charges a CSV into the table products does not work at all.

It was working fine until i added the variables prod_desc and prod_pic from a 3rd table.

Only with the categories was working fine.

Any suggestion?

CREATE TRIGGER triggername BEFORE INSERT ON products FOR EACH ROW BEGIN 
DECLARE name_cat VARCHAR( 255 ) ;
DECLARE prod_desc TEXT ;
DECLARE prod_pict  VARCHAR( 255 ) ;

SELECT category_name
INTO name_cat
FROM products_categories
WHERE cat_id = NEW.products_cat_id;

SELECT products_description, products_picture
INTO prod_desc, prod_pic
FROM products_description
WHERE products_id = NEW.products_id;

SET NEW.categories_name = name_cat, NEW.products_picture=prod_pic, NEW.products_description = prod_desc, NEW.Products_id=CONCAT('DEP',NEW.Products);

END
Was it helpful?

Solution

Is products_id unique in the products_description table?

If not, then that query against the products_description table could return more than one row, and if that happens, MySQL will throw an error.

You can add a LIMIT 1 clause to the query as a workaround, to prevent more than one row from being returned.

(I don't have any other explanation as to why the trigger is "working" from phpMyAdmin when you perform an INSERT, but is "not working" when a script performs an INSERT... if the trigger was "working" before you added a couple of local variables, a second query and some additional assignments. I don't see anything wrong with the trigger, doesn't appear to be a problem with local variable name collisions with column names. Syntax looks correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top