When user trying to edit his blog post and left some fields empty, he should get empty fields error, but, code that should do that get ignored and i get completely different error message instead.
My question: how to display empty fields error message properly?
Code snippets from editpost.php file:
1. Checking for empty fields:
// Check for empty fields
if (empty($title) || empty($body)) {
// Save correct data into fields
header('Location: editpost.php?error=emptyeditpostfield&title='.$title.'&body='.$body);
// Stop script
exit();
} else {
$query = "UPDATE posts SET title='$title', body='$body' WHERE id = {$update_id}";
}
2. Desired message:
<!-- Check if user has rights to editing post -->
<?php if (isset($_SESSION['id'])) : ?>
<?php if ($_SESSION['name'] == $post['author']) : ?>
<div class="container">
<h1>Edit Post</h1>
<?php
// Message I want to show
if (isset($_GET['error'])) {
if ($_GET['error'] == 'emptyeditpostfield') {
echo '<p class="text-warning">Fill in all fields!</p>';
}
}
?>
<!-- Edit post form... -->
3. Message I receive:
<!-- /Edit post form... -->
</div>
<?php else : header('Location: index.php?error=accessdenied'); exit(); // Message I got ?>
<?php endif; ?>
<?php else : header('Location: index.php?error=accessdenied'); exit(); ?>
<?php endif; ?>
Edited (Nov 30).
4. Code that is a part of this issue as well:
// Get ID
$id = mysqli_real_escape_string($conn, $_GET['id']);
// Create Query
$query = "SELECT * FROM posts WHERE id = $id";
// Get Result
$result = mysqli_query($conn, $query);
// Fetch Data
$post = mysqli_fetch_assoc($result);
// var_dump($posts);
// Free Result
mysqli_free_result($result);
// Close Connection
mysqli_close($conn);
Picture 1. Warnings after Submit with empty fields.
Picture 2. Edit post page.