我正试图将notice_title和notice_content插入通知表,并将category_type嵌入类别表 致命错误:未捕获异常"PDOException",消息为"SQLSTATE[23000]":完整性约束冲突:1048列"category_type"不能为null(位于C:''examplep''htdocs''add_notice.php中):17堆栈跟踪:#0 C:''examplep''tdocs''add_notice.php(17):PDOStatement->execute()#1{main}在第17行的C:''examplep'' htdocs''add_noticephp中抛出 MySQL add_notice.php 感谢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;
<?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>
首先,这个name="add_notice"
。表单不包含name属性,除非与jQuery一起使用,而且我在这里看不到jQuery。
此外,这里发生的情况是,这两个POST数组很可能是空的,正如@Drew在下面给出的答案中发布的那样"在显示表单之前插入空格"。
您需要对所有POST阵列使用!empty()
。执行该操作,或者删除约束并使用默认值。
使用错误报告。
参考文献:
- http://php.net/manual/en/function.empty.php
- http://php.net/manual/en/function.error-reporting.php
另外,你也错过了一个绑定。在初始查询中有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_title
和notice_content
为空的一个主要原因是:
您需要为以下内容添加值,并修复"隐藏"缺少"类型"的语法错误:
<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
绑定,尚不清楚。您需要将该值填入其中。
- 我相信我已经给了你足够的东西来让你的代码工作。现在由你来填补一些空白
我认为在插入notices
之前,你应该先插入categories
。
当你插入通知时,没有它引用的类别,所以约束有效。