Question

I have a table that contains products... something like

productname | price | category .... .... ...

category contains an INT value and it is the ID of the choosen category

id | categoryname | categorydescription ... ...

When I delete a Category (because this is a Ecommerce site I'm making in PHP, so the user can manage and delete categories at will), I want all the products whose Category is the Deleted Category ID, to set that value to 0, in other words, no category set.

I want to know how to do this with only MYSQL, and let me know if I should do it through PHP code.

Was it helpful?

Solution

I hope you can do with triggers. It's untested. It's from my archive. Please check.

CREATE TRIGGER del_cat_prod AFTER DELETE on category
FOR EACH ROW
BEGIN
update products set category_id  = 0 where category_id  = old.id;        
END

OTHER TIPS

Hope this will help you

UPDATE products SET category = 0 WHERE category = 'ur id' ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top