我建立它由3个复选框的调查程序和问题时比唐点击它提交答案,但最近我添加了一个外键,我的表,当我试图提交我得到这个错误答案:forgein关键问题
A Database Error Occurred
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`user_test`.`tblanswers`, CONSTRAINT `tblanswers_ibfk_1` FOREIGN KEY (`answerid`) REFERENCES `credentials` (`cid`) ON UPDATE CASCADE)
INSERT INTO `tblanswers` (`questionid`, `answerA`, `answerB`, `answerC`, `comment`, `cid`) VALUES ('melynas ', 'melynas ', 'baltas', 'geltonas', 'testas', NULL)
Filename: C:\wamp\www\Surva\system\database\DB_driver.php
Line Number: 330
任何人都可以帮我在这里解释我做错了什么?提前tnx。
表结构
我忘了提我使用笨。
凭证表
IF NOT EXISTS `credentials` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`second_name` varchar(255) NOT NULL,
`phone` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=98 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `credentials`
--
ALTER TABLE `credentials`
ADD CONSTRAINT `credentials_ibfk_1` FOREIGN KEY (`cid`) REFERENCES `tblanswers` (`answerid`) ON UPDATE CASCADE;
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */;
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */;
/*!40101 SET [email protected]_COLLATION_CONNECTION
tblanswers
CREATE TABLE IF NOT EXISTS `tblanswers` (
`answerid` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) DEFAULT NULL,
`cid` int(11) DEFAULT NULL,
`questionid` int(11) NOT NULL,
`answerA` varchar(255) NOT NULL,
`answerB` varchar(255) NOT NULL,
`answerC` varchar(255) NOT NULL,
`comment` varchar(255) NOT NULL,
PRIMARY KEY (`answerid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=205 ;
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */;
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */;
/*!40101 SET [email protected]_COLLATION_CONNECTION */;
你能显示这个查询的结论吗? 'DESC tblanswers'和'DESC credentials' – 2013-03-05 12:13:23
如果将外键放在当前表中,则不能在外列中插入记录,而不是在主列中。 – 2013-03-05 12:16:55
对不起,我畏惧明白我是新来的这 – 2013-03-05 12:17:08