Jump to content

Delete from multiple tables


kessels1234

Recommended Posts

Hi,

 

I want to perform a delete on multiple records from different tables.

 

I have a table called KLANTEN(=customers) and I have a table called REPAIR:

 

A customer can have more than 1 repairs. So when I delete a customer, also all repairs should be deleted to.

 

My tables:

1. KLANTEN

klant_id

title

firstname

lastname

etc.......

 

2. REPAIR

repair_id

klant_id

instrument

etc.....

 

I though that following sql statement would do the job but obvious it doesn't

case "klant":
		$repairsql = "DELETE FROM klant LEFT JOIN repair ON klant.klant_id=repair.klant_id WHERE klant_id = ".$validid.";";
		mysql_query($repairsql);
		if (!$repairsql){
		echo "Gegevens niet verwijderd";	
		}
		else{
		header("Location: ".$livesite. "/admin/klant_list.php");
		}
		break;

I get the $validid through the url via a $_GET

 

What am I doing wrong?

 

Thanks for your help in advance.

Danny

Link to comment
Share on other sites

Hi,

 

thanks for your help. I got it working and the final sql statement =

$repairsql = "DELETE klanten, repair  FROM klanten LEFT JOIN repair ON klanten.klant_id=repair.klant_id WHERE klanten.klant_id = ".$validid.";";

 

jl5501:Why wouldn't it be a good idea to do it this way?

 

Thanks again everyone

Link to comment
Share on other sites

It is just my opinion. Thinking about it, there is not really a problem, and it does have the advantage of being slightly more atomic, in that there cannot be a failure between the delete of the repairs entries and the main table.

 

Historically, I do not believe you were able to do this, which just goes to show that you are never to old to learn.

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.