Search the Community
Showing results for tags 'foreign'.
-
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