title: PHP register script with sha256 encryption
body: <p>I am making a register script that lets a user register for an account on a website. I decided to use sha256 to encrypt the password.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    // Set error message as blank upon arrival to page
  $errorMsg = "";
  // First we check to see if the form has been submitted 
  if (isset($_POST['Submit'])){
      //Connect to the database through our include 
      require_once ('includes/connect.inc.php');
      // Filter the posted variables
      $forename = $_POST['forename'];
      $surname = $_POST['surname'];
      $email = stripslashes($_POST['email']);
      $password = preg_replace("[^A-Za-z0-9]", "", $_POST['password']); // filter everything but numbers and letters
      $email = strip_tags($email);
      $town = preg_replace("[^A-Z a-z0-9]", "", $_POST['town']); // filter everything but spaces, numbers, and letters
  
      // Check to see if the user filled all fields with
      // the "Required"(*) symbol next to them in the join form
      // and print out to them what they have forgotten to put in
      if((!$forename) || (!$surname) || (!$email) || (!$password) || (!$town)){
  
          $errorMsg = "You did not submit the following required information!&lt;br /&gt;&lt;br /&gt;";
          if(!$forename){
              $errorMsg .= "--- Forename";
          } else if(!$surname){
              $errorMsg .= "--- Surname"; 
          } else if(!$email){ 
              $errorMsg .= "--- email"; 
         } else if(!$password){ 
             $errorMsg .= "--- password"; 
         } else if(!$town){ 
             $errorMsg .= "--- town"; 
         }
      } else {
  
             $hash = hash("sha256", $password);
  
              $sql = "INSERT INTO customers (forename, surname, email, password, town, registeredDate, active) 
              VALUES('$forename','$surname','$email', '$hash', '$town', GETDATE(), 'True')" ;
              $stmt2 = sqlsrv_query($conn,$sql);
  
  
    } // Close else after missing vars check
  } //Close if $_POST
  ?&gt;
  
  &lt;form action="join_form.php" method="post" enctype="multipart/form-data"&gt;
      &lt;tr&gt;
        &lt;td colspan="2"&gt;&lt;font color="#FF0000"&gt;&lt;?php echo "$errorMsg"; ?&gt;&lt;/font&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td width="163"&gt;&lt;div align="right"&gt;Forename:&lt;/div&gt;&lt;/td&gt;
        &lt;td width="409"&gt;&lt;input name="forename" type="text"/&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td width="163"&gt;&lt;div align="right"&gt;Surname:&lt;/div&gt;&lt;/td&gt;
        &lt;td width="409"&gt;&lt;input name="surname" type="text"/&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td&gt;&lt;div align="right"&gt;Email: &lt;/div&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input name="email" type="text" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
       &lt;tr&gt;
        &lt;td&gt;&lt;div align="right"&gt; Password: &lt;/div&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input name="password" type="password" /&gt; 
        &lt;font size="-2" color="#006600"&gt;(letters or numbers only, no spaces no symbols)&lt;/font&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td&gt;&lt;div align="right"&gt;Town: &lt;/div&gt;&lt;/td&gt;
        &lt;td&gt;
          &lt;input name="town" type="text" /&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
        &lt;td&gt;&lt;div align="right"&gt;&lt;/div&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="submit" name="Submit" value="Submit Form" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/form&gt;
  </code></pre>
  
  <p>When I press Submit button nothing happens. I don't get an error message, but the record does not get added to the database either.</p>
  
  <p>I know it has something to do with me using </p>
  
  <pre><code>$hash = hash("sha256", $password);
  </code></pre>
  
  <p>Maybe I put it in the wrong place or something? 
  I am very new to PHP.</p>