Jump to content

If I delete Brand the mobile related to that brand should also be deleted how to implement this?


Marybeauty

Recommended Posts

Using MySQL and PHP. I have two tables mobiles and brands. mobiles has { mobileID |brandID | price| BrandName| MobileName} brands has { brandID | BrandName } In admin panel admin is able to delete both the table indivdiually but I want that if brand is deleted all the related mobiles should also be deleted How is this possible?

Link to comment
Share on other sites

If you're looking for a PHP answer then what you do is make sure that your code deletes from both tables. You should also set up a foreign key constraint to make sure that you cannot delete brands without first deleting the mobiles.

If you're looking for a MySQL answer then you set up a foreign key with cascading deletes, so that deleting brands will automatically delete mobiles.

If you're looking for advice, do not delete data. If you don't want it around anymore then somehow mark or flag records you don't want to see, but still keep it.

Link to comment
Share on other sites

28 minutes ago, requinix said:

 If you're looking for a MySQL answer then you set up a foreign key with cascading deletes, so that deleting brands will automatically delete mobiles.

 

how to do this sorry but I am new.

Link to comment
Share on other sites

When in doubt, start by checking the documentation.

Basic syntax to add it to an existing table is

ALTER TABLE this_table ADD FOREIGN KEY (this_column, ...) REFERENCES other_table (other_column, ...) ON DELETE CASCADE

Foreign keys go on the table that is using the values from another table, so "this" table would be your mobiles table and the "other" one is the brands table.

Link to comment
Share on other sites

2 minutes ago, ginerjm said:

I hate to sound rude and crude, but if you are 'new' then why are you in charge of such a serious project?

I didn't see anything to suggest it's a serious project. How about giving them the benefit of the doubt?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.