title: How to validate form field in php
body: <p>I have a registration form that has some required field. i want to check if those required fields are filled and if they are filled correctly before i insert in my database. </p>
  
  <p>One of the required field is email, i also want to check if the email entered is a valid email.</p>
  
  <p>My code is below.</p>
  
  <p>Thanks in advance for your help, i really appreciate it.</p>
  
  <pre><code>&lt;?php
  include 'config.php';
  $tbl_name="citizens"; // Table name
  // Get values from form and formatting them as SQL strings
  $firstname = mysql_real_escape_string($_POST['firstname']);
  $middlename = mysql_real_escape_string($_POST['middlename']);
  $lastname = mysql_real_escape_string($_POST['lastname']);
  $sex = mysql_real_escape_string($_POST['sex']);
  $address = mysql_real_escape_string($_POST['address']);
  $employer = mysql_real_escape_string($_POST['employer']);
  $posincom = mysql_real_escape_string($_POST['posincom']);
  $states = mysql_real_escape_string($_POST['states']);
  $agerange = mysql_real_escape_string($_POST['agerange']);
  $income = mysql_real_escape_string($_POST['income']);
  $email = mysql_real_escape_string($_POST['email']);
  $phone = mysql_real_escape_string($_POST['phone']);
  
  // Insert data into mysql 
  $sql="INSERT INTO `$tbl_name` (firstname, middlename, lastname, sex, address, employer, position_in_company, states, age_range, local_govt_area, email, phone) VALUES('$firstname', '$middlename', '$lastname', '$sex', '$address', '$employer', '$posincom', '$states', '$agerange', '$income', '$email', '$phone')";
  $result=mysql_query($sql);
  
  // if successfully insert data into database, displays message "Successful". 
  if($result){
  echo "You Have Successful Registered";
  }else {
  echo "Sorry!!! Could Not Register You. All a* fields must be field.";
  }
  ?&gt;
  </code></pre>