unable to update a child table when a foreign key constraint is added: error :Cannot add or update a child row: a foreign key constraint fails
unable to update a child table when a foreign key constraint is added: error :Cannot add or update a child row: a foreign key constraint fails
拜托,我一直在尝试修复此错误,尝试了所有搜索和所有给出的解决方案,但 none 对我有用,这是我的 table。
CREATE TABLE `member` (
`member_id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(20) NOT NULL,
`first_name` varchar(20) NOT NULL,
`affiliation` varchar(20) NOT NULL,
`street_address` varchar(50) NOT NULL,
`city` varchar(20) NOT NULL,
`state` varchar(20) NOT NULL,
`zipcode` int(11) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`cell_number` varchar(15) NOT NULL,
`text` text NOT NULL,
`email` varchar(20) NOT NULL,
`permission_level` int(11) NOT NULL,
`role` varchar(12) NOT NULL,
PRIMARY KEY (`member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
CREATE TABLE `structure` (
`structure_id` int(11) NOT NULL AUTO_INCREMENT,
`structure_name` varchar(70) NOT NULL,
`year_built_from` date NOT NULL,
`year_built_to` date NOT NULL,
`builder` varchar(50) NOT NULL,
`original_use` varchar(70) NOT NULL,
`present_use` varchar(70) NOT NULL,
`floor_area` int(50) NOT NULL,
`levels` int(50) NOT NULL,
`doors` int(50) NOT NULL,
`windows` int(50) NOT NULL,
`rooms` int(50) NOT NULL,
`foundation_type` varchar(50) NOT NULL,
`construction_style` varchar(50) NOT NULL,
`joinery_style` varchar(50) NOT NULL,
`primary_wood_species` varchar(50) NOT NULL,
`condition` varchar(50) NOT NULL,
`coordinate_latitude` int(11) NOT NULL,
`coordinate_longitude` int(11) NOT NULL,
PRIMARY KEY (`structure_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
CREATE TABLE `location` (
`location_id` int(11) NOT NULL AUTO_INCREMENT,
`structure_id` int(11) NOT NULL,
`location_name` varchar(100) NOT NULL,
`legal_description` varchar(100) NOT NULL,
`township` varchar(100) NOT NULL,
`county` varchar(50) NOT NULL,
`state` varchar(50) NOT NULL,
`private` char(1) NOT NULL,
PRIMARY KEY (`location_id`),
KEY `structure_id` (`structure_id`),
CONSTRAINT `location_ibfk_1` FOREIGN KEY (`structure_id`) REFERENCES `structure` (`structure_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4
CREATE TABLE `owner` (
`owner_id` int(11) NOT NULL AUTO_INCREMENT,
`structure_id` int(11) NOT NULL,
`member_id` int(11) NOT NULL,
PRIMARY KEY (`owner_id`),
KEY `structure_id` (`structure_id`),
KEY `member_id` (`member_id`),
CONSTRAINT `owner_ibfk_1` FOREIGN KEY (`member_id`) REFERENCES `member` (`member_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `owner_ibfk_2` FOREIGN KEY (`structure_id`) REFERENCES `structure` (`structure_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
我想要实现的是让所有者 table 显示成员的 ID 和结构 table,这样如果您单击成员的 ID,它将带您到成员,同样前往位置 table。但是一旦我在表单中输入我的数据并提交,只有成员 table 和结构 table 会得到它们的内容,位置和所有者 table 上不会有任何内容,但是当我删除location 上的外键,并重新提交相同的表单,location 现在将获得自己的内容,只留下没有与表单直接联系的字段的所有者。
这里是php代码
<?php
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt_member = $conn->prepare("INSERT INTO `member`(` last_name`, `first_name`, `affiliation`, `street_address`, `city`, `state`, `zipcode`, `phone_number`, `cell_number`, `text`, `email`, `permission_level`, `role`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt_member->bind_param("ssssssissssis",$member_id, $last_name, $first_name, $affilition, $street_address, $city, $state,$zipcode, $phone_number, $cell_number, $text, $email, $permission_level, $role);
// set parameters and execute
$last_name = $_POST['lname'];
$first_name = $_POST['fname'];
$affilition = $_POST['affiliation'];
$street_address = $_POST['street_address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$phone_number = $_POST['phone_number'];
$cell_number = $_POST['cell_number'];
$text = $_POST['text'];
$email = $_POST['email'];
$permission_level = $_POST['permission_level'];
$role = $_POST['role'];
$stmt_member->execute();
$stmt_member->close();
// prepare and bind
$stmt_structure = $conn->prepare("INSERT INTO `structure`( `structure_name`, `year_built_from`, `year_built_to`, `builder`, `original_use`, `present_use`, `floor_area`, `levels`, `doors`, `windows`, `rooms`, `foundation_type`, `construction_style`, `joinery_style`, `primary_wood_species`, `condition`, `coordinate_latitude`, `coordinate_longitude`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt_structure->bind_param("ssssssiiiiisssssii",$structure_id, $structure_name, $year_built_from, $year_built_to, $builder, $original_use, $present_use, $floor_area, $levels, $doors, $windows, $rooms, $foundation_type, $construction_style, $joinery_style, $primary_wood_species, $condition, $coordinate_latitude, $coordinate_longitude);
// set parameters and execute
$structure_name = $_POST['structure_name'];
$year_built_from = $_POST['year_built_from'];
$year_built_to = $_POST['year_built_to'];
$builder = $_POST['builder'];
$original_use = $_POST['original_use'];
$present_use = $_POST['present_use'];
$floor_area = $_POST['floor_area'];
$levels = $_POST['levels'];
$doors = $_POST['doors'];
$windows = $_POST['windows'];
$rooms = $_POST['rooms'];
$foundation_type = $_POST['foundation_type'];
$construction_style = $_POST['construction_style'];
$joinery_style = $_POST['joinery_style'];
$primary_wood_species = $_POST['primary_wood_species'];
$condition = $_POST['condition'];
$coordinate_latitude = $_POST['coordinate_latitude'];
$coordinate_longitude = $_POST['coordinate_longitude'];
$stmt_structure->execute();
$stmt_structure->close();
// prepare and bind
$stmt_location = $conn->prepare("INSERT INTO `location`( `location_name`, `legal_description`, `township`, `county`, `state`, `private`) VALUES (?, ?, ?, ?, ?, ?)");
$stmt_location->bind_param("ssssss", $location_name, $legal_description, $township, $county, $state, $private);
// set parameters and execute
$location_name = $_POST['location_name'];
$legal_description = $_POST['legal_description'];
$township = $_POST['township'];
$county = $_POST['county'];
$state = $_POST['state'];
$private = $_POST['private'];
$stmt_location->execute();
$stmt_location->close();
echo "New records created successfully";
$conn->close();
?>
很抱歉post。
提前谢谢大家
发生这种情况是因为您无法插入没有结构的位置。
要插入新位置,您必须确保在插入语句中传递了 structure_id 的值,在您的情况下不会发生这种情况。
让我们看看您的插入语句:
INSERT INTO `location`( `location_name`, `legal_description`, `township`, `county`, `state`, `private`) Values(?,?,?,?,?,?);
如果您注意到,您的查询中缺少 structure_id,因此它总是失败。
要修复它,只需添加它。
INSERT INTO `location`( `structure_id`,`location_name`, `legal_description`, `township`, `county`, `state`, `private`) Values(?,?,?,?,?,?,?);
我们仍然需要获取最后插入的 structure_id,因为它在我们的代码中不可用,所以在您的 php 代码中的这一行之后
$stmt_structure->执行();输入以下内容:
$structure_id =$conn->insert_id;
拜托,我一直在尝试修复此错误,尝试了所有搜索和所有给出的解决方案,但 none 对我有用,这是我的 table。
CREATE TABLE `member` (
`member_id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(20) NOT NULL,
`first_name` varchar(20) NOT NULL,
`affiliation` varchar(20) NOT NULL,
`street_address` varchar(50) NOT NULL,
`city` varchar(20) NOT NULL,
`state` varchar(20) NOT NULL,
`zipcode` int(11) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`cell_number` varchar(15) NOT NULL,
`text` text NOT NULL,
`email` varchar(20) NOT NULL,
`permission_level` int(11) NOT NULL,
`role` varchar(12) NOT NULL,
PRIMARY KEY (`member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
CREATE TABLE `structure` (
`structure_id` int(11) NOT NULL AUTO_INCREMENT,
`structure_name` varchar(70) NOT NULL,
`year_built_from` date NOT NULL,
`year_built_to` date NOT NULL,
`builder` varchar(50) NOT NULL,
`original_use` varchar(70) NOT NULL,
`present_use` varchar(70) NOT NULL,
`floor_area` int(50) NOT NULL,
`levels` int(50) NOT NULL,
`doors` int(50) NOT NULL,
`windows` int(50) NOT NULL,
`rooms` int(50) NOT NULL,
`foundation_type` varchar(50) NOT NULL,
`construction_style` varchar(50) NOT NULL,
`joinery_style` varchar(50) NOT NULL,
`primary_wood_species` varchar(50) NOT NULL,
`condition` varchar(50) NOT NULL,
`coordinate_latitude` int(11) NOT NULL,
`coordinate_longitude` int(11) NOT NULL,
PRIMARY KEY (`structure_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
CREATE TABLE `location` (
`location_id` int(11) NOT NULL AUTO_INCREMENT,
`structure_id` int(11) NOT NULL,
`location_name` varchar(100) NOT NULL,
`legal_description` varchar(100) NOT NULL,
`township` varchar(100) NOT NULL,
`county` varchar(50) NOT NULL,
`state` varchar(50) NOT NULL,
`private` char(1) NOT NULL,
PRIMARY KEY (`location_id`),
KEY `structure_id` (`structure_id`),
CONSTRAINT `location_ibfk_1` FOREIGN KEY (`structure_id`) REFERENCES `structure` (`structure_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4
CREATE TABLE `owner` (
`owner_id` int(11) NOT NULL AUTO_INCREMENT,
`structure_id` int(11) NOT NULL,
`member_id` int(11) NOT NULL,
PRIMARY KEY (`owner_id`),
KEY `structure_id` (`structure_id`),
KEY `member_id` (`member_id`),
CONSTRAINT `owner_ibfk_1` FOREIGN KEY (`member_id`) REFERENCES `member` (`member_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `owner_ibfk_2` FOREIGN KEY (`structure_id`) REFERENCES `structure` (`structure_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
我想要实现的是让所有者 table 显示成员的 ID 和结构 table,这样如果您单击成员的 ID,它将带您到成员,同样前往位置 table。但是一旦我在表单中输入我的数据并提交,只有成员 table 和结构 table 会得到它们的内容,位置和所有者 table 上不会有任何内容,但是当我删除location 上的外键,并重新提交相同的表单,location 现在将获得自己的内容,只留下没有与表单直接联系的字段的所有者。
这里是php代码
<?php
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt_member = $conn->prepare("INSERT INTO `member`(` last_name`, `first_name`, `affiliation`, `street_address`, `city`, `state`, `zipcode`, `phone_number`, `cell_number`, `text`, `email`, `permission_level`, `role`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt_member->bind_param("ssssssissssis",$member_id, $last_name, $first_name, $affilition, $street_address, $city, $state,$zipcode, $phone_number, $cell_number, $text, $email, $permission_level, $role);
// set parameters and execute
$last_name = $_POST['lname'];
$first_name = $_POST['fname'];
$affilition = $_POST['affiliation'];
$street_address = $_POST['street_address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$phone_number = $_POST['phone_number'];
$cell_number = $_POST['cell_number'];
$text = $_POST['text'];
$email = $_POST['email'];
$permission_level = $_POST['permission_level'];
$role = $_POST['role'];
$stmt_member->execute();
$stmt_member->close();
// prepare and bind
$stmt_structure = $conn->prepare("INSERT INTO `structure`( `structure_name`, `year_built_from`, `year_built_to`, `builder`, `original_use`, `present_use`, `floor_area`, `levels`, `doors`, `windows`, `rooms`, `foundation_type`, `construction_style`, `joinery_style`, `primary_wood_species`, `condition`, `coordinate_latitude`, `coordinate_longitude`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt_structure->bind_param("ssssssiiiiisssssii",$structure_id, $structure_name, $year_built_from, $year_built_to, $builder, $original_use, $present_use, $floor_area, $levels, $doors, $windows, $rooms, $foundation_type, $construction_style, $joinery_style, $primary_wood_species, $condition, $coordinate_latitude, $coordinate_longitude);
// set parameters and execute
$structure_name = $_POST['structure_name'];
$year_built_from = $_POST['year_built_from'];
$year_built_to = $_POST['year_built_to'];
$builder = $_POST['builder'];
$original_use = $_POST['original_use'];
$present_use = $_POST['present_use'];
$floor_area = $_POST['floor_area'];
$levels = $_POST['levels'];
$doors = $_POST['doors'];
$windows = $_POST['windows'];
$rooms = $_POST['rooms'];
$foundation_type = $_POST['foundation_type'];
$construction_style = $_POST['construction_style'];
$joinery_style = $_POST['joinery_style'];
$primary_wood_species = $_POST['primary_wood_species'];
$condition = $_POST['condition'];
$coordinate_latitude = $_POST['coordinate_latitude'];
$coordinate_longitude = $_POST['coordinate_longitude'];
$stmt_structure->execute();
$stmt_structure->close();
// prepare and bind
$stmt_location = $conn->prepare("INSERT INTO `location`( `location_name`, `legal_description`, `township`, `county`, `state`, `private`) VALUES (?, ?, ?, ?, ?, ?)");
$stmt_location->bind_param("ssssss", $location_name, $legal_description, $township, $county, $state, $private);
// set parameters and execute
$location_name = $_POST['location_name'];
$legal_description = $_POST['legal_description'];
$township = $_POST['township'];
$county = $_POST['county'];
$state = $_POST['state'];
$private = $_POST['private'];
$stmt_location->execute();
$stmt_location->close();
echo "New records created successfully";
$conn->close();
?>
很抱歉post。 提前谢谢大家
发生这种情况是因为您无法插入没有结构的位置。
要插入新位置,您必须确保在插入语句中传递了 structure_id 的值,在您的情况下不会发生这种情况。
让我们看看您的插入语句:
INSERT INTO `location`( `location_name`, `legal_description`, `township`, `county`, `state`, `private`) Values(?,?,?,?,?,?);
如果您注意到,您的查询中缺少 structure_id,因此它总是失败。
要修复它,只需添加它。
INSERT INTO `location`( `structure_id`,`location_name`, `legal_description`, `township`, `county`, `state`, `private`) Values(?,?,?,?,?,?,?);
我们仍然需要获取最后插入的 structure_id,因为它在我们的代码中不可用,所以在您的 php 代码中的这一行之后 $stmt_structure->执行();输入以下内容:
$structure_id =$conn->insert_id;