我正在构建一个调查应用程序,它包含 3 个复选框和一个问题,当单击按钮时它会提交答案,但最近我在表中添加了一个外键,当我尝试提交答案时出现此错误:
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 提前。
表结构
我忘了提到我正在使用codeigniter。
凭据表
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 CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION
答案
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 CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;