我正在尝试将数据库中填充的选择字段中的值传递给具有外键列的表
表格
<?php
require ('aaa2/conn/auth.php');
$select = "SELECT * FROM aaa_categories";
$res = mysqli_query($conn, $select) or die(mysqli_error($conn));
if (mysqli_num_rows($res)) {
echo "<select name='aaa_cat' class='form-control' id='aaa-cat' required>";
echo "<option selected>--Select a category that best fits your petition--</option>";
while ($row = mysqli_fetch_array($res)) {
echo '<option value="'.$row['aaa_categories_id'].'">'.$row['aaa_categories_name'].'</option>';
}
echo "</select>";
}
?>
处理器
$aaa_details = mysqli_real_escape_string($conn, $_POST['aaa_details']);
$aaa_cat = mysqli_real_escape_string($conn, $_POST['aaa_cat']);
$query = "INSERT INTO aaa (aaa_detail, aaa_cat)VALUES ('$aaa_details', '$aaa_cat')";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
架构
CREATE TABLE IF NOT EXISTS `aaa_db`.`aaa` (
`aaaid` INT NOT NULL AUTO_INCREMENT,
`userid` INT NOT NULL,
`aaa_categories_id` INT NOT NULL,
`aaa_detail` VARCHAR(350) NULL,
PRIMARY KEY (`aaaid`),
INDEX `fk_aaa_users1_idx` (`userid` ASC),
INDEX `fk_aaa_aaa_categories1_idx` (`aaa_categories_id` ASC),
CONSTRAINT `fk_aaa_users1`
FOREIGN KEY (`userid`)
REFERENCES `aaa_db`.`users` (`userid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_aaa_aaa_categories1`
FOREIGN KEY (`aaa_categories_id`)
REFERENCES `aaa_db`.`aaa_categories` (`aaa_categories_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
错误
Cannot add or update a child row: a foreign key constraint fails (`aaa_db`.`aaa`, CONSTRAINT `fk_aaa_aaa_categories1` FOREIGN KEY (`aaa_categories_id`) REFERENCES `aaa_categories` (`aaa_categories_id`) ON DELETE NO ACTION )
我一直在努力奋斗3天,我很感激任何建议。感谢
答案 0 :(得分:0)
您的查询
INSERT INTO aaa(aaa_detail,aaa_cat)VALUES('$ aaa_details','$ aaa_cat')
除非aaa_cat == aaa_categories_id ,否则似乎与您的实际架构不对应
如果是这种情况,你得到的错误很奇怪。如果它抱怨forconstraint“fk_aaa_users1”,我会理解它,因为你没有在相关专栏中插入任何内容。
请查看您发布的信息并提出建议。然后我会相应地更新这个答案......