title: Validating user input using php
body: <p>I'm new to php and mysql and I'm trying to check if a user has entered something into a a coupls of textboxes and to also check if what has been entered is string. I want to do a check before posting to the database. I also want the html form to retain the value initially entered by the user. Please how do i achieve this. </p>
  
  <p>Here's what I've done so far. This works but it still shows that the data has been entered successfully.</p>
  
  <pre><code>if(isset($_POST['register'])){  
  //PHP FIELD VALIDATIONS
  if($_POST['fname']==""){
      echo "First name is required &lt;br/&gt;";
  
  }
  else{
     $fname= filter_var($_POST['fname'], FILTER_SANITIZE_STRING);
  }
  if($_POST['lname']==""){
      echo "Last name is required &lt;br/&gt;";
  }
  else{
      $lname= $_POST['lname'];
  }
  if($_POST['email']==""){
      echo "Email address is required &lt;br/&gt;";
  }
  else{
       $email= $_POST['email'];
  }
  if($_POST['pword']==""){
      echo "Password is required&lt;br/&gt;";
  }
  else{
        $pword= $_POST['pword'];
  }  
  
  $fname=mysql_real_escape_string($fname);
  $lname=mysql_real_escape_string($lname);
  $email=mysql_real_escape_string($email);
  $pword=mysql_real_escape_string($pword);
  
  require_once 'scripts/connect_to_mysql.php';
  $sql = "INSERT INTO customer ".
         "(First_name,Last_name, Email, Password, date_added) ".
         "VALUES('$fname','$lname','$email','$pword', NOW())";
         //echo $sql;
  mysql_select_db('online_store');
  $result = mysql_query( $sql, $conn );
  if(! $result )
  
  {
    die('Could not enter data: ' . mysql_error());
  }
  echo "&lt;span style='color:green;'&gt;Entered data successfully&lt;/span&gt;";
  mysql_close($conn);
  }
  ?&gt;
  </code></pre>