Jump to content

Foreign key constraint fails


josephbupe

Recommended Posts

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

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.