josephbupe Posted March 20, 2013 Share Posted March 20, 2013 Hi,I am attempting to insert a foreign key into a table along with other information, but I am getting an error message: Could not insert data into DataBase: Cannot add or update a child row: a foreign key constraint fails (`collectionsdb2`.`collections`, CONSTRAINT `collections_ibfk_1` FOREIGN KEY (`cat_id`) REFERENCES `categories` (`cat_id`)) The insert statement is as follows: $sql = "INSERT INTO collections (cat_id, ctitle, csubject, creference, cmaterial, ctechnic, cwidth, cheight, cperiod, cmarkings, cdescription, csource, cartist, cfilename) VALUES ($cat_id, '$ctitle', '$csubject', '$creference', '$cmaterial', '$ctechnic', '$cwidth', '$cheight', '$cperiod', '$cmarkings', '$cdescription', '$csource', '$cartist', '" . $image['name'] . "')"; $result = mysql_query($sql) or die ("Could not insert data into DataBase: " . mysql_error()); exit; And my two tables are as folows: /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; DROP TABLE IF EXISTS collectionsdb2.collections; CREATE TABLE `collections` ( `c_id` int(11) NOT NULL AUTO_INCREMENT, `cat_id` int(4) DEFAULT NULL, `ctitle` varchar(65) NOT NULL, `csubject` varchar(65) DEFAULT NULL, `creference` varchar(65) DEFAULT NULL, `cyear` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `cmaterial` varchar(65) DEFAULT NULL, `ctechnic` varchar(255) DEFAULT NULL, `cwidth` varchar(65) DEFAULT NULL, `cheight` varchar(65) DEFAULT NULL, `clength` varchar(65) DEFAULT NULL, `cdiameter` varchar(65) DEFAULT NULL, `cperiod` varchar(65) DEFAULT NULL, `cmarkings` varchar(255) DEFAULT NULL, `cdescription` text, `csource` varchar(65) DEFAULT NULL, `cartist` varchar(65) DEFAULT NULL, `cfilename` varchar(65) DEFAULT NULL, PRIMARY KEY (`c_id`), KEY `cat_id` (`cat_id`), CONSTRAINT `collections_ibfk_1` FOREIGN KEY (`cat_id`) REFERENCES `categories` (`cat_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; and /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; DROP TABLE IF EXISTS collectionsdb2.categories; CREATE TABLE `categories` ( `cat_id` int(4) NOT NULL AUTO_INCREMENT, `category` varchar(25) NOT NULL, PRIMARY KEY (`cat_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; I am using a select option box to insert foreign key (category) into the table form the categories table. Please, how should I resolve this issue? Thanx. joseph Quote Link to comment Share on other sites More sharing options...
josephbupe Posted March 20, 2013 Author Share Posted March 20, 2013 Resolved. Thanx Quote Link to comment Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 Thanks for telling future visitors who search Google for this problem exactly what the solution was. I'm going to guess you were trying to insert a cat_id value into collections that did not exist the category table. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.