![]() |
Delete records from multiple tables? (PHP, MySQL)
I have to delete records in multiple table i have only main category id "1001" and i want to delete all sub categories and products.
Database Structure Main Category Table:- MainID = 1001 MainName = HP Sub Category Table:- MainID = 1001 SubID = 2001 SubName = laptops Products Table:- SubID = 2001 ProID = 3001 ProName = HP G60t series _______________ any idea? |
You could configure the relationships and then use cascading delete.
|
is it not possible without relationships?????
Thank's for your reply |
While Cascade Delete is your best option here then I think there IS a way to go about it, it's far from glamorous, actually what I'm about to code is pretty bad practice, but go easy, I'm new :-P
PHP Code:
You have the MainID, so get every record in the subcategories table where the MainID matches that ID, store these in an array $subArray. Then for every item in $subArray, delete from the Products table every record that has the same SubID as the current item in the array. Then you delete every record in the subcategories table that matches the MainID. If I'm not mistaken then that means every SubCategory of that particular category will be deleted and every product in all of the guilty subcategories will also be deleted. Now I'm not a great PHP coder, so I'm waiting for someone to come and prove me wrong, but I guess a great way to learn is to be told where you're going wrong! (See why Cascade Delete is better? :-P ) |
Quote:
Thank’s its working fine.i have already developed same code for delete records. I just want to more stretch my code its very long i'm feeling its not professional work any advise! |
| All times are GMT. The time now is 09:11 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0