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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.