<?php
// post_add.php
if( isset ($_POST['title']) && ($_POST['body']) && !empty($_POST) ){
require 'connection.php';
$stmt = $conn->prepare('INSERT INTO posts (title,body) VALUES (:title, :body)');
$stmt->bindValue(':title', $_POST['title']);
$stmt->bindValue(':body', $_POST['body']);
$stmt->execute();
echo 'Entry posted. <a href="post_view.php?id='.$conn->lastInsertId().'">View</a>';
}else if(empty($_POST['title']) || empty($_POST['body']) ){
echo "no values entered";
}
?>
As my code is , empty fields are never submitted to my database which was my problem previously and with some research I concorted this code .Now i'm trying to inform the user that the required fields were not filled if the click the submit button before entering anything in them.My code ends up displaying my pseudo (echo) error message right away when the file is loaded, i tried redirects(which were horrible), the joys of being a PHP newbie :).I don't require answers but guidance on how to come to a solution.