如何显示空字段错误消息

I have a page which allows the user to "create a topic", open submitting this the form goes to another through a verification process which inserts the topic into the database and re-directs to back to the main page. However I want my verification page "add topic" to display an error message if all fields are not filled in. here is a my code, please can you tell me where I would need to add this validation code to notify the user to fill all fields:

// get data that sent from form 
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];

$datetime=date("d/m/y h:i:s"); //create date time





$sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
$result=mysql_query($sql);




if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {

echo "ERROR";
}
mysql_close();

My suggestion would be create a separate php file called validation and inside the validation file add a function. Of course you can create this function inside the same php file. If you made the separate use an include statement to place it on your page. Also a quick post-back to itself would be good since you could easily be able to get access to the posted variables and already be on the page to show errors. Otherwise you would have to return the Errors in a get, post or session. If everything was successful you could post or redirect right after the postback (maybe to a success page) and the user would only see the postback if errors present.

include_once("Validation.php"); 

as shown above.

validateNewTopic($topic, $detail, $name, $email, $datetime)
{


}

Then inside you could use if statements to check conditions. If you want a quick solution you can create a variable to hold all the errors.

    $Error = "<p class='errors'">;

    if ($topic == "")
    {
     $Error+="topic is required";
    }


    if ($Error != "<p class='errors'">)
    {

        return $Error +"</p>";

    }
else
{
return "";
}

Since you are posting the values you can catch them in a variable on postback to validate.

$topic = $POST['topic'];

$Error=validateNewTopic($topic);

if ($Error != "")
{
  ?>
echo $Error
<?php
}
else {
//run sql code and show success
}

By putting the paragraph tags inside the $Error messages we can just echo and it will already be in the paragraph tag with the class errors. You can make it prettier by using an un-ordered list and when adding an error using list items. I'm not sure how familiar you are with php but at anytime you can stop writing php code by closing the tags. (< php ?> and reopen < ? php) as shown above in the if statement. I know this was not 100% clear but this is something you should try/research and practice since it is used so often. Good luck!

You can send the error to the main page by using php GET request, and then display it.