I'm displaying multiple queries and if
statements. In the if
statement, it increments notice_category_id
by 1 each time a category is created, but when I code the update/delete/edit (or if truncated), sections won't rowCount()
increment the row and make duplicates. I'm just wondering if I should change the way the the notice_category_id
is working. If so, any ideas? Also, how can I display each error message near the field that has the error using the $assNoticeError
and $addNoticeErrorMessage
?
I was just wondering if someone could help me make this code simpler.
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(empty($_POST["form"])) {
$user_id = $_SESSION['user_id'];
$query0 = $connection->prepare("SELECT
user_id
FROM users
");
$query0->execute();
$query1 = $connection->prepare("SELECT
notice_category_id
FROM notices
");
$query1->execute();
$notice_category_id = $query1->rowCount();
if($notice_category_id <= $notice_category_id) {
$notice_category_id++;
};
if($_SERVER["REQUEST_METHOD"] == "POST") {
$addNoticeError = array();
$addNoticeErrorMessage = "";
$addNoticeSuccessMessage = "";
if(empty($_POST["notice_title"])) {
$addNoticeError[] = "You must enter a Notice Title.";
}
if(empty($_POST["notice_content"])) {
$addNoticeError[] = "You must enter a Notice Description.";
}
if(empty($_POST["category_type"])) {
$addNoticeError[] = "You must select a Category Type.";
}
if(!empty($addNoticeError)) {
foreach($addNoticeError as $error) {
$addNoticeErrorMessage .= $error . '<br />';
}
} else {
if(!isset($error)) {
$category_type = $_POST['category_type'];
$query2 = "INSERT INTO categories (category_type) VALUES (:category_type)";
$query2 = $connection->prepare($query2);
$query2->bindParam(":category_type", $_POST["category_type"], PDO::PARAM_STR, 255);
$query2->execute();
$notice_category_id = $_POST['notice_category_id'];
$notice_user_id = $_POST['notice_user_id'];
$notice_title = $_POST['notice_title'];
$notice_content = $_POST['notice_content'];
$query3 = "INSERT INTO notices (notice_category_id, notice_user_id, notice_title, notice_content) VALUES (:notice_category_id, :notice_user_id, :notice_title, :notice_content)";
$query3 = $connection->prepare($query3);
$query3->bindParam(":notice_category_id" , $_POST["notice_category_id"] , PDO::PARAM_INT, 3);
$query3->bindParam(":notice_user_id" , $_POST["notice_user_id"] , PDO::PARAM_INT, 3);
$query3->bindParam(":notice_title" , $_POST["notice_title"] , PDO::PARAM_STR, 255);
$query3->bindParam(":notice_content" , $_POST["notice_content"] , PDO::PARAM_STR, 500);
$query3->execute();
header("Location: notices.php");
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Add a Notice</title>
</head>
<body>
<?php if(!empty($addNoticeErrorMessage)) : ?>
<p><?php echo $addNoticeErrorMessage ?></p>
<?php endif ?>
<?php if(!empty($addNoticeSuccessMessage)) : ?>
<p><?php echo $addNoticeSuccessMessage ?></p>
<?php endif ?>
<h4>Add a Notice</h4>
<form method="post" name="add_notice" action="">
<input name="notice_category_id" type="hidden" value="<?php echo $notice_category_id; ?>" />
<input name="notice_user_id" type="hidden" value="<?php echo $user_id; ?>" />
<input type="text" name="notice_title" id="notice_title" placeholder="notice title" maxlength="25" value="<?php if(isset($_POST["notice_title"])) { echo htmlentities($_POST["notice_title"]); } ?>" />
<br />
<textarea name="notice_content" id="notice_content" placeholder="notice content"/><?php if(isset($_POST["notice_content"])) { echo htmlentities($_POST["notice_content"]); } ?></textarea>
<br /><br />
<select name="category_type">
<option value="">Select...</option>
<option value="Content1">Content1</option>
<option value="Content2">Content2</option>
<option value="Content3">Content3</option>
<option value="Content4">Content4</option>
<option value="Content5">Content5</option>
</select>
<br />
<br />
<input type="hidden" name="add_notice">
<button type="submit" id="btnSubmit" name="btnSubmit">Add a new Notice</button>
</form>
</body>
</html>