Jump to content

Problem on validation with php


giannis

Recommended Posts

Hello Guys, great forum, first time posting here :-)

 

I'm creating a php registration form, and I'm doing some validations. The problem on my code is the following:

 

When the password is mimatched with the password_again, the following if is executed (referring the email validation). But when the password is matched with the password_again, the email validation isn't running. Why php is not executing the if when the passwords are OK?

<?php
if(!empty($_POST['username']) && !empty($_POST['password']))
  {
  $username = mysql_real_escape_string($_POST['username']);
  $password = md5(mysql_real_escape_string($_POST['password']));
  $email = mysql_real_escape_string($_POST['email']);
  
  $check_username = mysql_query("SELECT * FROM users WHERE username = '".$username."'");
  
    if(mysql_num_rows($check_username) == 1)
      {
      echo "<h1>Error</h1>";
      echo "<p> Username is already in use, click <a href=\"register.php> here</a> to try again!</p>";
      }
      if($_POST['password'] === $_POST['password_again'])
        {
        return true;
        }
      else
        {
        echo "Passwords do not match. Please click <a href=\"register.php\">here</a> to try again<br/>";
        }
      if (isset($_POST['email']))
         {
      if (isValidEmail($_POST['email']))
            {
            return true;
            }
      else
            {
            echo "<p><tr><td>The email: ".$_POST['email']." is invalid!</td></tr> Please click <a href=\"register.php\">here</a> to try again</p>";
            }
         }
    else
      {
      $write = mysql_query("INSERT INTO users (username, password, email) VALUES('".$username."', '".$password."', '".$email."')");
        if($write)
        {
        echo "<p> Account created! Click <a href=\"index.php\">here</a> to login.</p>";
        echo "<meta http-equiv='refresh' content='=4;index.php' />";
        }
        else
        {
        echo "<p> There was an error. Please click <a href=\"register.php\">here</a> to try again.</p>";
        }
       }
   }
   else
   {
      ?>

 

Link to comment
https://forums.phpfreaks.com/topic/212020-problem-on-validation-with-php/
Share on other sites

      if($_POST['password'] === $_POST['password_again'])
        {
        return true;
        }

 

That code - which I copied from your original post - says: "If the passwords match, RETURN - leave - quit - do NOT run the email check which is the next statement"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.