title: PHP Server Side Form Validation.Empty Form fields are inserted into database
body: <p>I am new to PHP and am trying to do Server Side Form Validation. There are two PHP files <code>Login.php</code> and <code>Form.php</code>. Registration is done in <code>Login.php</code> and Validation in <code>Form.php</code>. The idea is that <code>Form.php</code> will process the form data sent by <code>Login.php</code></p>
  
  <p>My problem: even if <code>form</code> fields are empty, the variables are still being inserted into the database.</p>
  
  <p>I don't want to insert if its empty. Rather, it has to route back to <code>Login.php</code> with error messages stored as a session variable. </p>
  
  <p>I have checked the <code>Form</code> fields using <code>!isset()</code> and <code>empty</code> in <code>Form.php</code>  using an <code>if..else</code> clause. In the <code>if..else</code> clause you can find out if the form fields are empty, and if so, they must go the session variable clause (inside the <code>if</code> condition). Instead, it is going to the <code>else</code> condition and inserting the empty values in variables <code>('$username','$password','$phone','$mailid','$city')</code> in to the database.</p>
  
  <p>I have read previous questions for similar problem here and even checked Youtube for Server Side Validation. What did I do wrong? Is there a problem with the use of session variables. Kindly assist</p>
  
  <p>Login.php:</p>
  
  <pre><code>&lt;!Doctype HTML&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;link rel="stylesheet" type="text/css" href= "Form.css" /&gt;
  &lt;script src="Form.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;?php
      session_start();
      $passworderr='';    
      if(isset($_SESSION["passworderr"])) {
          $passworderr=$_SESSION["passworderr"];
      }
  ?&gt;
  
   &lt;div id="Outer"&gt;
   &lt;div id="left" &gt;
    &lt;form action="/DatabaseDrivenWebpage/Form.php" method="POST"     name="form"&gt;
   &lt;p&gt;&lt;label&gt;Username&lt;/label&gt; &lt;input type="text" name="regusername"    placeholder="Your name"/&gt; &lt;/p&gt;
   &lt;p&gt;&lt;label&gt;Password&lt;/label&gt; &lt;input type="text" name="regpassword" placeholder="Password"/&gt; &lt;/p&gt;
   &lt;input type="Submit" value="Login" /&gt;
   &lt;/form&gt;
   &lt;/div&gt;
  
   &lt;div id="right"&gt;
   &lt;form action="/DatabaseDrivenWebpage/Form.php" method="POST" id="formm"&gt;
   &lt;p&gt;*Username &lt;input required name="username" type="text" /&gt;&lt;?php  //echo $usernameerr;?&gt;&lt;/p&gt;
   &lt;p&gt;*Password &lt;input name="password" type="password" /&gt; &lt;?php echo $passworderr;?&gt;&lt;/p&gt; 
   &lt;p&gt;   *Phone &lt;input name="phone" type="tel" /&gt;&lt;?php //echo $phoneerr;?&gt;&lt;/p&gt;
  &lt;p&gt;  *MailId &lt;input name="mailid" type="email" /&gt;&lt;?php //echo $mailiderr;?&gt;&lt;/p&gt;
  &lt;p&gt;    *City &lt;input name="city" type="text" /&gt;&lt;?php //echo $cityerr;?&gt;&lt;/p&gt;
    &lt;input type="Submit" value="Signup" /&gt;
  &lt;/form&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>Form.php:</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
     $dbservername='localhost';$dbname='mani';$dbusername='root';$dbpassword='';
  
  $dbconn=mysqli_connect($dbservername,$dbusername,$dbpassword);
   if(!$dbconn){
  die("Connection failed:". mysqli_connect_error());
  }
  
   if(!isset($_POST["username"])) {
       $_SESSION["usernameerr"]="UserName is required";
   }
   else{
       $username=mysqli_real_escape_string($dbconn,$_POST["username"]);
   }
   if(!isset($_POST["password"])) {
       $_SESSION["passworderr"]="Enter a password";
   }
   else{
       $password=mysqli_real_escape_string($dbconn,$_POST["password"]);
   }
   if(!isset($_POST["phone"])) {
       $_SESSION["phoneerr"]="Phone number is required";
   } 
   else{
       $phone=mysqli_real_escape_string($dbconn,$_POST["phone"]);
   }
   if(!isset($_POST["mailid"])) {
       $_SESSION["mailiderr"]="Enter a valid mail id";
   } 
   else{
       $mailid=mysqli_real_escape_string($dbconn,$_POST["mailid"]);
   }
   if(!isset($_POST["city"])) {
       $_SESSION["cityerr"]="Enter your resident city";
   } 
   else{
       $city=mysqli_real_escape_string($dbconn,$_POST["city"]);
   }
  
   $selected = mysqli_select_db($dbconn,"$dbname")
   or die("Could not select examples".mysqli_error($dbconn));
  
   if(isset($_POST["username"]) and isset($_POST["password"]) and isset($_POST["phone"]) and isset($_POST["mailid"]) and isset($_POST["city"]) )
   {
       $res=mysqli_query($dbconn,"Insert into   user(username,password,phone,mailid,city) values('$username','$password','$phone','$mailid','$city')");
       if($res)
       {
           header("location:Login.php");
       }
   }
   else
   {
   print "Problem in inserting";
   header("location:Login.php");
   } 
  
   mysqli_close($dbconn);
   ?&gt;
  </code></pre>