title: Empty Form Field Validation Issues - Cannot Write Into Database
body: <p>I am following a tutorial from W3schools which checks for empty entries in a form then displays the errors next to each field e.g. if a user omits their forename and surname beside each filed "enter your forename" and "enter your surname" is displayed.</p>
  
  <p>What they don't explain is how then to write the data into a table. I know how to do this but I cannot get it working with their script.</p>
  
  <p>Here's the full code. Sorry for the confusion.</p>
  
  <pre><code>&lt;?php
  session_start();
  include "connect.php";
  error_reporting(E_ERROR);
  $fnameErr = $snameErr = $unameErr = $passwordErr = $emailErr = "";
  $forename = $surname = $username = $password = $email = "";
  if ($_SERVER["REQUEST_METHOD"] == "POST")
  {
     if (empty($_POST["forename"]))
       {$fnameErr = "Enter your forename";}
     else
       {$forename = validate_input($_POST["forename"]);}
     if (empty($_POST["surname"]))
       {$snameErr = "Enter your surname";}
     else
       {$surname = validate_input($_POST["surname"]);}
     if (empty($_POST["username"]))
       {$unameErr = "Enter a username";}
     else
       {$username = validate_input($_POST["username"]);}
     if (empty($_POST["password"]))
       {$passwordErr = "Enter a password";}
     else
       {$password = validate_input($_POST["password"]);}
     if (empty($_POST["email"]))
       {$emailErr = "Enter your email address";}
     else
       {$email = validate_input($_POST["email"]);}
  }
  function validate_input($data)
  {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
  }
  else {
  $Link = mysql_connect($Host, $User, $Password);
  $Query = "INSERT INTO $Table_2 VALUES ('0','".mysql_escape_string('forename')."','".mysql_escape_string("surname")."', '".mysql_escape_string("username")."', '".mysql_escape_string("password")."', '".mysql_escape_string("email")."')";
  if(mysql_db_query ($DBName, $Query, $Link)){
  $message = "You have successfully registered";
  header("Location: register.php?message=$message");
  }else{
  $message = "You've Broke It!";
  die(mysql_error());
  header("Location: register.php?message=$message");
  }
  ?&gt;
  
  &lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  
  &lt;link rel="stylesheet" href="styles/all.css" /&gt;
  &lt;link rel="stylesheet" href="styles/contact.css" /&gt;
  
  &lt;script type="text/javascript" src="javascript/jquery-1.7.1.min.js"&gt;&lt;/script&gt;
  
  &lt;link href='//fonts.googleapis.com/css?family=Ubuntu:400,500' rel='stylesheet' type='text/css'&gt;
  &lt;link href='//fonts.googleapis.com/css?family=Voltaire' rel='stylesheet' type='text/css'&gt;
  
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /&gt;
  
  &lt;/head&gt;
  
  &lt;div class="container"&gt;
  
  &lt;form action="register.php? method="post" enctype="multipart/form-data"&gt;
  
  &lt;fieldset&gt;
  &lt;legend&gt;&lt;span class="formheading"&gt;Registration Form&lt;/span&gt;&lt;/legend&gt;
  &lt;div class="form"&gt;
  
  &lt;div&gt;&lt;p class="formtext"&gt;Please enter your forename&lt;/p&gt;&lt;label for="forename"&gt;Forename&lt;/label&gt;&lt;input id="forename" class="inset" name="forename" type="text"&gt;
  &lt;?php echo $fnameErr;?&gt;
  &lt;/div&gt;
  &lt;div&gt;&lt;p class="formtext"&gt;Please enter your surname&lt;/p&gt;&lt;label for="surname"&gt;Surname&lt;/label&gt;&lt;input id="surname" class="inset" name="surname" type="text"&gt;
  &lt;?php echo $snameErr;?&gt;&lt;/div&gt;
  &lt;div&gt;&lt;p class="formtext"&gt;Please enter a username&lt;/p&gt;&lt;label for="username"&gt;Username&lt;/label&gt;&lt;input id="username" class="inset" name="username" type="text"&gt;&lt;/div&gt;
  &lt;?php echo $usernameErr;?&gt;
  &lt;div&gt;&lt;p class="formtext"&gt;Please enter a password&lt;/p&gt;&lt;label for="password"&gt;Password&lt;/label&gt;&lt;input id="password" class="inset" name="password" type="text"&gt;&lt;/div&gt;
  &lt;?php echo $passwordErr;?&gt;
  &lt;div&gt;&lt;p class="formtext"&gt;Please enter your e-mail address&lt;/p&gt;&lt;label for="email"&gt;email address&lt;/label&gt;&lt;input id="email" class="inset" name="email" type="text"&gt;
  &lt;?php echo $emailErr;?&gt;&lt;/div&gt;
  
  &lt;div class="submit"&gt;&lt;input name="Submit" type="Submit" class="submitbtn" value="Submit" /&gt;&lt;/div&gt;
  &lt;div class="submit"&gt;&lt;input name="Reset" type="Reset" class="resetbtn" value="Reset"/&gt;&lt;/div&gt;
  
  &lt;/div&gt;
  &lt;/fieldset&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I couldn't insert with aconrad insert statement but got it working with this.</p>
  
  <pre><code>$Link = mysql_connect($Host, $User, $Password);
  $Query = "INSERT INTO $Table_2 VALUES ('0','".mysql_escape_string($forename)."','".mysql_escape_string($surname)."', '".mysql_escape_string($username)."', '".mysql_escape_string($password)."', '".mysql_escape_string($email)."')";
  </code></pre>
  
  <p>If I enter :</p>
  
  <pre><code>forename
  surname
  username
  password
  </code></pre>
  
  <p>But do not enter anything in the email field then click submit the errors are displayed but all fields are cleared and the user has to re-enter all the information. How do I prevent this from happening i.e. how do I  keep the data that has been entered?</p>