View Single Post
Old 07-15-2009, 10:30 PM   #5 (permalink)
shoaibmunir
The Wanderer
 
shoaibmunir's Avatar
 
Join Date: Jun 2009
Location: Dubai, UAE
Posts: 23
Thanks: 2
shoaibmunir is on a distinguished road
Default

Quote:
Originally Posted by ScottRiley View Post
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

PHP Code:
<?php

$subQuery 
"SELECT * FROM subcategories WHERE MainID=1001";
$subResult mysql_query($subQuery);
while(
$subArray mysql_fetch_array($subResult)) {
   
$qDelFromProd "DELETE * FROM Products WHERE SubID=".$subArray['SubID'];
    
$rDelFromProd mysql_query($qDelFromProd);
}
$qDelFromSub "DELETE * FROM subcategories WHERE MainID=1001";
$rDelFromSub mysql_query($qDelFromSub);


?>
Or something similar, I'm not 100% sure if that code will work I haven't got the time to check it! But the idea behind it is:

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? )


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!
__________________
==========================================
http://paysha.com/Pakistan/Sialkot/Profiles/100171.html
==========================================
Send a message via MSN to shoaibmunir
shoaibmunir is offline  
Reply With Quote