php约束违规错误1452

时间:2016-07-25 05:09:45

标签: php mysql sql pdo

早上好, 当我试图在我的数据库中使用此pdo插入一个事件时,我有一个很大的问题,这个SQL错误:

<小时/> SQL:

$service_url="https://api.servicem8.com/api_1.0/Attachment/9640887b-df46-4bfb-a47c-4afb20ed3d6b.file";
$file_data = file_get_contents("/tmp/cool_document.pdf"); // read the file contents from disk

$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_USERPWD, "user@domain.com:p@55w0rd");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true); // This configures CURL to send a HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $file_data); // This puts your PDF file in the body of the HTTP request
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);


SQL错误关注:

  

完整性约束违规:1452无法添加或更新子级   row:外键约束失败($subsquery1 = "SELECT cat_id FROM categories WHERE cat_name = ".$event_cat; $subsquery2 = "SELECT tournament_id FROM tournaments WHERE tournament_name = ".$event_tournament; $sqlx = "INSERT INTO events(event_team1, event_team2, event_cat, event_tournament, event_start_at, event_end_to, event_by) VALUES(:event_team1, :event_team2, :event_cat, :event_tournament, :event_start_at, :event_end_to, :event_by)"; // Prepare statement $statementx = $pdo->prepare($sqlx); // execute the query $resultx = $statementx->execute(array(':event_team1' => $event_team1, ':event_team2' => $event_team2, ':event_cat'=> $subsquery1, ':event_tournament'=> $subsquery2, ':event_start_at' => $event_start_at, ':event_end_to' => $event_end_to,':event_by'=>$event_by)); datenbank,CONSTRAINT   events外键(events_ibfk_1)参考event_cat   (categories)ON UPETE CASCADE ON UPDATE CASCADE)

cat_id

- 表-- Table structure for table `events` CREATE TABLE `events` ( `event_id` int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY, `event_team1` varchar(255) NOT NULL, `event_team2` varchar(255) NOT NULL, `event_start_at` timestamp NOT NULL, `event_end_to` timestamp NOT NULL, `event_cat` int(8) NOT NULL, `event_by` int(8) NOT NULL, `event_tournament` int(8) NOT NULL, KEY `event_cat` (`event_cat`), KEY `event_by` (`event_by`), KEY `event_tournament` (`event_tournament`) )ENGINE=InnoDB DEFAULT CHARSET=latin1;

的约束
posts

- 表ALTER TABLE `posts` ADD CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`post_event`) REFERENCES `events` (`event_id`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `posts_ibfk_2` FOREIGN KEY (`post_by`) REFERENCES `users` (`id`) ON UPDATE CASCADE;

的约束
events

请有人帮助......

1 个答案:

答案 0 :(得分:1)

  

完整性约束违规:1452无法添加或更新子行:   外键约束失败(datenbank.events,   CONSTRAINTevents_ibfk_1FOREIGN KEY(event_cat)   REFERENCEScategories(CAT_ID)

您正在对表events执行插入操作,并将值放在event_cat列中。

categories中的表cat_id中尚不存在该值。

你说它必须。所以db引擎说它不会这样做。它忠实地遵守你的命令。