TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-23-2009, 02:59 PM   #1 (permalink)
The Wanderer
 
shoaibmunir's Avatar
 
Join Date: Jun 2009
Location: Dubai, UAE
Posts: 23
Thanks: 2
shoaibmunir is on a distinguished road
Help 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?
Send a message via MSN to shoaibmunir
shoaibmunir is offline  
Reply With Quote
Old 06-23-2009, 03:53 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You could configure the relationships and then use cascading delete.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-29-2009, 11:00 AM   #3 (permalink)
The Wanderer
 
shoaibmunir's Avatar
 
Join Date: Jun 2009
Location: Dubai, UAE
Posts: 23
Thanks: 2
shoaibmunir is on a distinguished road
Default

is it not possible without relationships?????


Thank's for your reply
__________________
==========================================
http://paysha.com/Pakistan/Sialkot/Profiles/100171.html
==========================================
Send a message via MSN to shoaibmunir
shoaibmunir is offline  
Reply With Quote
Old 06-29-2009, 09:56 PM   #4 (permalink)
The Wanderer
 
ScottRiley's Avatar
 
Join Date: Jun 2009
Location: Liverpool, UK
Posts: 7
Thanks: 1
ScottRiley is on a distinguished road
Default

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? )
Send a message via ICQ to ScottRiley Send a message via AIM to ScottRiley Send a message via MSN to ScottRiley
ScottRiley is offline  
Reply With Quote
The Following User Says Thank You to ScottRiley For This Useful Post:
shoaibmunir (07-15-2009)
Old 07-15-2009, 09:30 PM   #5 (permalink)
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
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search query multiple tables? (PHP, MySQL) shoaibmunir General 6 06-16-2009 01:47 PM
Help on PHP MYSQL Forms Input deesudesu Absolute Beginners 1 03-20-2009 10:50 AM
PHP & MySQL Inserting multiple entires from range. Acrylic Absolute Beginners 3 10-02-2008 02:27 AM
Securing your MySQL Queries with Sprintf Wildhoney General 26 03-18-2008 06:52 PM
Error in connecting to MySQL via PHP EyeDentify MySQL & Databases 0 01-03-2008 01:06 PM


All times are GMT. The time now is 02:07 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design