无法添加或更新子行:外键约束失败

Cannot add or update a child row: a foreign key constraint fails

我正在尝试 INSERT notice_titlenotice_content 进入 通知 tablecategory_type 进入 类别 table 但我收到以下错误,无法解决。

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'category_type' cannot be null' in C:\xampp\htdocs\add_notice.php:17 Stack trace: #0 C:\xampp\htdocs\add_notice.php(17): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\add_notice.php on line 17

MySQL

CREATE TABLE `categories`
(
     `category_id` INT(3) NOT NULL AUTO_INCREMENT, 
     `category_type` VARCHAR(255) NOT NULL, 
      PRIMARY KEY (`category_id`)
)     ENGINE = InnoDB;

CREATE TABLE `notices`
(
     `notice_id` INT(3) NOT NULL AUTO_INCREMENT, 
     `notice_category_id` INT(3) NOT NULL, 
     `notice_user_id` INT(3) NOT NULL, 
     `notice_title` VARCHAR(255) NOT NULL, 
     `notice_content` VARCHAR(500) NOT NULL, 
     `notice_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 
      PRIMARY KEY (`notice_id`), 
      FOREIGN KEY (`notice_category_id`)
           REFERENCES categories(`category_id`), 
      FOREIGN KEY (`notice_user_id`)
           REFERENCES users(`user_id`)
)     ENGINE = InnoDB;

add_notice.php

<?php
session_start();

include_once("database/connect.php");

if(isset($_POST['add_notice'])) {
    $notice_id      = $_POST['notice_id'];
    $notice_title   = $_POST['notice_title'];
    $notice_content= $_POST['notice_content'];
    $category_id    = $_POST['category_id'];
    $category_type  = $_POST['category_type'];
}

$query1 = "INSERT INTO categories (category_id, category_type) VALUES (:category_id, :category_type)";
$query1 = $connection->prepare($query1);
$query1->bindParam(":category_id", $_POST["category_id"]);
$query1->bindParam(":category_type", $_POST["category_type"]);
$query1->execute();

$query2 = "INSERT INTO notices (notice_id, notice_title, notice_content, notice_category_id) VALUES (:notice_id, :notice_title, :notice_content, :notice_category_id)";
$query2 = $connection->prepare($query2);
$query2->bindParam(":notice_id", $_POST["notice_id"]);
$query2->bindParam(":notice_title", $_POST["notice_title"]);
$query2->bindParam(":notice_content", $_POST["notice_content"]);
$query2->execute();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Create a Notice</title>
</head>
<body>
    <h4>Create a Notice</h4>
        <form method="post" name="add_notice" action="add_notice.php">
            <input name="notice_id" hidden />
            <input name="category_id" hidden />

            <input type="text" name="notice_title" />
            <br />
            <textarea name="notice_content" /></textarea>
            <br /><br />
            <label>Category:</label>
                <select name="category_type">
                    <option value="">Select...</option>
                    <option value="Content1">Content1</option>
                    <option value="Content2">Content2</option>
                    <option value="Content3">Content3</option>
                </select>
            <br />
            <button type="submit" name="submit">Create a Notice</button>
        </form>
</body>

谢谢

我认为你应该先插入 categories,然后再插入 notices

当你插入通知时,没有它引用的类别,所以约束有效。

首先,这个name="add_notice"。除非与 jQuery 一起使用,否则表单不包含名称属性,我在这里看不到 jQuery。

此外,这里发生的事情是这 2 个 POST 数组很可能是空的,就像@Drew 在下面给出的答案中发布的那样。 "the inserts happen with blanks before the form is shown"

您需要对所有 POST 数组使用 !empty()。要么这样做,要么删除约束并使用默认值。

使用错误报告。

参考文献:

此外,您还缺少此处的绑定。您的初始查询中有 4 个,但只有 3 个在准备查询后发生绑定。

$query2 = "INSERT INTO notices (notice_id, notice_title, notice_content, notice_category_id) VALUES (:notice_id, :notice_title, :notice_content, :notice_category_id)";
$query2 = $connection->prepare($query2);
$query2->bindParam(":notice_id", $_POST["notice_id"]);
$query2->bindParam(":notice_title", $_POST["notice_title"]);
$query2->bindParam(":notice_content", $_POST["notice_content"]);
$query2->execute();

由于 notice_id 是 AI,您需要将其从查询中删除并添加此处缺少的 :notice_category_id

notice_titlenotice_content 为空的一个主要原因是:

您需要为以下内容添加值并修复 "hidden" 缺少 "type" 的语法错误:

<input name="notice_id" hidden />
<input name="category_id" hidden />

应该看起来像这样:

<input name="notice_id" type="hidden" value="example_value_x" />
<input name="category_id" type="hidden" value="example_value_y" />

注: value="example_value_x"value="example_value_y"为代表值。您将需要为其各自的值填写那个。

至于:notice_category_id绑定使用哪一个,不得而知。您将需要填写那个对应的值。

  • 我相信我已经给了你足够的代码来让你的代码工作。现在由您来填补一些空白。