title: Logic for form validation before insert into database
body: <p>I would like to write a logic for data validation before insert into database.  If the data not valid, then it will prompt user errors, but then I facing problem which not the logic that I wish:
  (1) Message "Data successfully inserted!" shown even the error checking message was prompt.
  (2) Message "Data successfully inserted!" shown even no data was entered in the form then click submit.</p>
  
  <p>How should I change the logic to the one that I wish to have?</p>
  
  <pre><code>&lt;?php
  // Initialize variables to null.
  $comp_nameError ="";
  $compLicenseeNameError ="";
  
  if(isset($_POST['comp_name']))  {$comp_name= $_POST['comp_name'];}
  if(isset($_POST['comp_licensee_name'])) {$comp_licensee_name= $_POST['comp_licensee_name'];}
  
  //On submitting form below function will execute
  if (isset($_POST['submit'])) {
  
      function test_input($data) {
          $data = trim($data);
          $data = stripslashes($data);
          $data = htmlspecialchars($data);
      return $data;
      }           
  
      //-------------------------Form Validation Start---------------------//
     if (empty($_POST["comp_name"])) {
       $comp_nameError = "Name is required";
     } else {
       $comp_name = test_input($_POST["comp_name"]);
       // check name only contains letters and whitespace
       if (!preg_match("/^[a-zA-Z ]*$/",$comp_name)) {
         $comp_nameError = "Only letters and white space allowed"; 
       }
     }
  
     if (empty($_POST["comp_licensee_name"])) {
       $compLicenseeNameError = "Company Licensee Name is required";
     } else {
       $comp_licensee_name = test_input($_POST["comp_licensee_name"]);
     }
     //-------------------------Form Validation End---------------------//
  
  
      // attempt a connection
      $host="host=xx.xx.xx.xx";
      $port="port=xxxx";
      $dbname="dbname=xxxx";
      $credentials="user=xxxxxx password=xxxxxxx";
  
      $dbh = pg_connect("$host $port $dbname $credentials");
      if (!$dbh) {
          die("Error in connection: " . pg_last_error());
      }
  
      // execute query
      $sql = "INSERT INTO t_comp(comp_name, comp_licensee_name)VALUES('$comp_name', '$comp_licensee_name')";
      $result = pg_query($dbh, $sql); 
  
      if (!$result) {
          die("Error in SQL query: " . pg_last_error());
      }
      echo "Data successfully inserted!";
  
      // free memory
      pg_free_result($result); 
  
      // close connection
      pg_close($dbh);
  }
  //php code ends here 
  ?&gt;
  
  
  &lt;html&gt;
      &lt;head&gt;
  
      &lt;link rel="stylesheet" href="style/style.css" /&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
          &lt;div class="maindiv"&gt;
              &lt;div class="form_div"&gt;    
              &lt;form method="post" action="compReg.php"&gt;                   
                  &lt;span class="error"&gt;* required field.&lt;/span&gt;
  
                  &lt;br&gt;
                  &lt;hr/&gt;
                  &lt;br&gt;
                  Company Name:&lt;br&gt;&lt;input class="input" type="text" name="comp_name" value=""&gt;
                  &lt;span class="error"&gt;* &lt;?php echo $comp_nameError;?&gt;&lt;/span&gt;
                  &lt;br&gt;         
  
                  Company Licensee:&lt;br&gt;&lt;input class="input" type="text" name="comp_licensee_name" value=""&gt;
                  &lt;span class="error"&gt;* &lt;?php echo $compLicenseeNameError;?&gt;&lt;/span&gt;
                  &lt;br&gt;    
  
                  &lt;input class="submit" type="submit" name="submit" value="Submit"&gt; 
              &lt;/form&gt;
              &lt;/div&gt;          
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>