duanhuren5581 2015-08-03 14:27
浏览 141

select to foreign key column:无法添加或更新子行:外键约束失败

I am trying to pass values from a select field populated from the database to a table which has a foreign key column

The form

 <?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>";
                                }
 ?>

The Processor

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

The Schema

    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;

The Error

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 )

I have been struggling with this for 3 days now, I appreciate any advice. Thanks

  • 写回答

1条回答 默认 最新

  • douqian5553 2015-08-03 15:14
    关注

    Your query

    INSERT INTO aaa (aaa_detail, aaa_cat)VALUES ('$aaa_details', '$aaa_cat')

    doesn't seem to correspond to your actual schema unless aaa_cat == aaa_categories_id

    If that is the case the error you get is strange. I would understand it if it was complaining about forconstraint "fk_aaa_users1", as you are not inserting anything in the relevant column.

    Please review the information you posted and advise. I will then update this answer accordingly ...

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行