I'm trying to INSERT the notice_title and notice_content into the notices table and the category_type into the categories table but I get the following error below and can't work it out.
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>
Thanks