Jump to content

Deleting multiple database entries across different tables


shure2

Recommended Posts

Hi,

 

I'm trying to delete a company, which will then delete all of its products, which will then delete all the product qualities and product details.

 

The tables I have are company, product, productdetail, productquality.

 

I could get the company and its product to delete because the 'compid' is stored in both tables, but only the 'prodid' is stored in the productquality and productdetail so somehow I need to get the right prodid into a variable so I can use it to delete entries from the productquality and productdetail tables. i dont have any table relationships at the moment

 

Below is the code I have:

 

<?php
    require "session_logincheck.php";
    require "connect.php";
    
    $compID = $_GET['compid'];
   
    $productID = "select prodid from product where prodcompid = '$compID'";
    
    $query =  "delete from company where compid = '$compID'";
    $query1 =  "delete from product where prodcompid = '$compID'";
    $query2 =  "delete from productdetail where prodid = '$result0'";
    $query3 =  "delete from productquality where prodid = '$result0'";

    $result0 = @mysql_query($productID);
    $result = @mysql_query($query, $connection) or die ("Unable to perform query<br>$query".mysql_error());
    $result1 = @mysql_query($query1, $connection) or die ("Unable to perform query<br>$query1".mysql_error());
    $result2 = @mysql_query($query2, $connection) or die ("Unable to perform query<br>$query2".mysql_error());
    $result3 = @mysql_query($query3, $connection) or die ("Unable to perform query<br>$query3".mysql_error());

    
    header("Location: company_page.php");
    exit();
?>

 

Link to comment
Share on other sites

$query =  "DELETE c, p, pd, pq
    FROM company c
    JOIN product p ON c.compid = p.prodcompid
    JOIN productdetail pd ON prod.id = pd.prodid
    JOIN productquality pq ON prod.id = pq.prodid
    WHERE compid = '$compID'";

 

That query is wrong as there is no table with the name prod as for your InnoDB it might give problems. Create a SELECT query that selects all the data that needs to be deleted then transform it in a DELETE query using my code as a base

Link to comment
Share on other sites

/quote]

That query is wrong as there is no table with the name prod as for your InnoDB it might give problems. Create a SELECT query that selects all the data that needs to be deleted then transform it in a DELETE query using my code as a base

 

sorry yes I spotted this and corrected it so it works, however I find that if I don't have productqualities and productdetails relating to products and then a product relating to a company nothing will deleted (I would like it do that even if there were no products or productdetails/product qualities the company would still be removed.

 

thank you for your patience, really appreciate it, I can feel how close I am to cracking this now!

 

 

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.