Jump to content

Recommended Posts

Hi, this is more MySQL rather than PHP but i am trying to delete a row, and it has a foreign key field in it. This foreign key is linked to another table which has this as its primary key.

 

I get this error message

Cannot delete or update a parent row: a foreign key constraint fails ()

 

Have I put the FK on the table the wrong way around? I think i added the FK to the child table, rather than the parent. Am i supposed to add it to the parent instead?

Link to comment
https://forums.phpfreaks.com/topic/162501-force-delete/
Share on other sites

Corrected:

Hi, this is more MySQL rather than PHP but i am trying to delete a row, and it has a foreign key field in it. This foreign key is linked to another table which has this as its primary key.

 

I get this error message

Cannot delete or update a parent row: a foreign key constraint fails ()

 

Have I put the FK on the table the wrong way around? I think i added the FK to the parent table, rather than the child. Am i supposed to add it to the child table instead?

 

No rows are coming up.. Im getting an empty set.

 

I ran this statement..

 

select address.Country from address left join country on (address.Country=country.Country) where country.Country is not null and address.Country is null;

 

In this case address is the parent table, this is the record I am trying to delete.

 

country is the other table with the PK in it.

 

What i was using to delete the record was:

DELETE FROM address where Address_ID='ADID00000002';

 

Do i need to add more to this statement?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/162501-force-delete/#findComment-857689
Share on other sites

My bad here, i had another table which had this row as a FK. i deleted that record, then i could delete this one...

 

Do you know how to do an if statement to run some code if an empty set is returned?

 

if (empty? (mysql_result("SELECT Address_ID FROM address ORDER BY Address_ID")))

{

<code>

}

Link to comment
https://forums.phpfreaks.com/topic/162501-force-delete/#findComment-857699
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.