php constraint violation Error 1452

2020-05-09 22:56发布

Good morning, i have a big problem with this sql error when i,m trying to insert a event in my database with this pdo:


SQL:

$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));


SQL ERROR FOLLOW :

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (datenbank.events, CONSTRAINT events_ibfk_1 FOREIGN KEY (event_cat) REFERENCES categories (cat_id) ON DELETE CASCADE ON UPDATE CASCADE)

-- 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;

-- Constraints for table 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;

-- Constraints for table events

ALTER TABLE `events` ADD CONSTRAINT `events_ibfk_1` FOREIGN KEY (`event_cat`) REFERENCES `categories` (`cat_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `events_ibfk_2` FOREIGN KEY (`event_by`) REFERENCES `users` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `events_ibfk_3` FOREIGN KEY (`event_tournament`) REFERENCES `tournaments` (`tournament_id`) ON DELETE CASCADE ON UPDATE CASCADE;

Please could somebody helps by ...

标签: php mysql sql pdo
1条回答
别忘想泡老子
2楼-- · 2020-05-09 23:29

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (datenbank.events, CONSTRAINTevents_ibfk_1FOREIGN KEY (event_cat) REFERENCEScategories(cat_id)

You are performing an insert to table events with a value being placed in column event_cat.

That value does not already exist in table categories in the column cat_id.

And you said it must. So the db engine says it won't do it. It is faithfully obeying your orders.

查看更多
登录 后发表回答