Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have three tables in my database. All three are tied together by a foreign key referencing the main table. Main table (CompanyData) has primary key (companyID). Two child tables (RepData and ContactData) each have foreign keys (ID_company) that references the main table's primary key.

CompanyData owns ContactData (OnetoOne) and CompanyData owns RepData (OnetoMany).

Now, I can physically add to each table using phpAdmin with no problem. However, I can not do it through a form. The form is working and was just fine before I added the relationships. Since then, I get:

"Cannot add or update a child row: a foreign key constraint fails (`reviewmy_jos1`.`dataContact`, CONSTRAINT `FK_contactid_companyid` FOREIGN KEY (`id_company`) REFERENCES `dataCompany` (`companyID`) ON DELETE CASCADE ON UPDATE CASCADE) "

What I don't understand, how could it work perfectly fine through phpAdmin when inserting the data but not when inserted through the form?

Also, if it makes a difference I'm using MariaDB.

What I have tried:

Works in phpAdmin when inserting data does not work in form when inserting data since adding the relationships.
Posted
Updated 11-Apr-17 23:58pm

1 solution

Without the benefit of seeing your code, I can only assume that the order you are inserting data into the tables from the form is incorrect.

You cannot add rows to RepData nor ContactData until ID_company exists (as companyID) on the CompanyData table
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900