title: PHP - PDO - Form Validation - Insert statement gets executed even when errors exist
body: <p>I didn't notice it until I finished the validation but I realized that even when my errors appear on top of my form box I then go to phpmyadmin and I look at the data, and even if I purposely added errors the form will be submitted.</p>
  
  <p>Then my second problem including the one stated above, no matter what I do the student Id or "anum" is not posting. it continues to give me a "0" value in the students table in my database.</p>
  
  <p>This is the entire code: </p>
  
  <pre><code>&lt;?php
  //Starting session
  session_start();
  
  // Validation starts here
  if (empty($_POST) === false) {
      $errors   = array();
      $anum     = $_POST['anum'];
      $first    = $_POST['first'];
      $last     = $_POST['last'];
      $why      = $_POST['why'];
      $comments = $_POST['comments'];
  
      if (empty($anum) === true || empty($first) === true || empty($last) === true) {
          $errors[] = 'Form is incomplete please revise it!';
      } else {
  
          if (ctype_alnum($anum) === false) {
              $errors[] = 'A number can only consist of alphanumeric characters!';
          }
          if ((strlen($anum) &lt; 9) &amp;&amp; (strlen($anum)) &gt; 9) {
              $errors[] = 'A number is incorrect!';
          }
          if (ctype_alpha($first) === false) {
              $errors[] = 'First mame must only contain alphabetical characters!';
          }
          if (ctype_alpha($last) === false) {
              $errors[] = 'Last name must only contain alphabetical characters!';
          }
          if (empty($why))
              $errors[] = 'Please make sure to select the proper reasoning for your vistit today!';
  
          elseif ($why === 'Other') {
  
              if (empty($comments))
                  $errors[] = 'Please explain the nature of your visit in the comments box!';
  
              else {
  
                  if (strlen($comments) &lt; 15)
                      $errors[] = 'Your explaination is short, please revise!';
  
                  if (strlen($comments) &gt; 45)
                      $errors[] = 'Your explaintion is to long, please revise!';
  
              }
  
          }
  
          if (empty($errors) === false) {
              header('location: signedin.php');
              exit();
          }
  
          // Validations ends here
  
          $host     = "localhost"; // Host name
          $username = "root"; // Mysql username
          $password = "testdbpass"; // Mysql password
          $db_name  = "test"; // Database name
  
          // Connect to server via PHP Data Object
          $dbh = new PDO("mysql:host=localhost;dbname=test;", $username, $password);
          $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
          try {
              $query = $dbh-&gt;prepare("INSERT INTO `students` (anum, FIRST, LAST, why, comments)
                                     VALUES (:anum, :FIRST, :LAST, :why, :comments)");
  
              $query-&gt;execute(
  
                  array(
                      'anum'     =&gt; $_POST['anum'],
                      'first'    =&gt; $_POST['first'],
                      'last'     =&gt; $_POST['last'],
                      'why'      =&gt; $_POST['why'],
                      'comments' =&gt; $_POST['comments']
                  ));
          } catch (PDOException $e) {
              error_log($e-&gt;getMessage());
              die($e-&gt;getMessage());
          }
          $dbh = null;
  
      }
  
  }
  ?&gt;
  
  &lt;html&gt;
  &lt;body&gt;
  &lt;title&gt;Student Signin Form&lt;/title&gt;
  &lt;table width="300" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"&gt;
      &lt;tr&gt;
          &lt;?php
          if (empty($errors) === false) {
              echo '&lt;h3&gt;';
              foreach ($errors as $error) {
                  echo '&lt;center&gt;&lt;li&gt;', $error, '&lt;/li&gt;&lt;/center&gt;';
              }
  
              echo '&lt;h3&gt;';
          }
          ?&gt;
      &lt;form action="" method="post"&gt;
          &lt;td&gt;
              &lt;table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"&gt;
                  &lt;tr&gt;
  
                  &lt;tr colspan="3"&gt;
                      &lt;center&gt;&lt;/center&gt;
                      &lt;strong&gt;Student Signin Form&lt;/strong&gt;&lt;/tr&gt;
                  &lt;p&gt;Student ID Number: &lt;input type="text" name="anum" &lt;?php if (isset($_POST['anum']) === true) {
                          echo 'value="', $_POST['anum'], '"';
                      } ?&gt; /&gt;
  
                  &lt;p&gt;First Name: &lt;input type="text" name="first" &lt;?php if (isset($_POST['first']) === true) {
                          echo 'value="', $_POST['first'], '"';
                      } ?&gt; /&gt;
  
                  &lt;p&gt;Last Name: &lt;input type="text" name="last" &lt;?php if (isset($_POST['last']) === true) {
                          echo 'value="', $_POST['last'], '"';
                      } ?&gt; /&gt;
  
                  &lt;p&gt;How may we help you? &lt;select name="why"/&gt;
                      &lt;option value=""&gt;&lt;/option&gt;
                      &lt;option value="Appeal"&gt;Appeal&lt;/option&gt;
                      &lt;option value="Other"&gt;Other: Please specify the nature of your visit bellow&lt;/option&gt;
                      &lt;/select&gt;
                      &lt;/tr&gt;
  
  
                      &lt;br&gt;
  
                  &lt;P&gt;If other please describe the issue you are having.&lt;/P&gt;
                  &lt;textarea rows="10" cols="50" name="comments" &lt;?php if (isset($_POST['comments']) === true) {
                      echo 'value="', $_POST['comments'], '"';
                  } ?&gt;&gt;&lt;/textarea&gt;
  
  
                  &lt;input type="submit" name="submit" value="Send"/&gt;
  
      &lt;/form&gt;
  
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>