Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this code I am validating registration form, if all requirements are fullfilled data is successfully inserted into database and I am redirected to login page,but it user fails to comply with requirements though page refreshes but no error is shown neither the registration form appears just a blank white page. Whats wrong in this code
PHP
 <?php
  error_reporting('E_ALL ^ E_NOTICE');
 if(isset($_POST['reg'])){
   $fn = ucfirst($_POST['fname']);
   $ln = ucfirst($_POST['lname']);
   $un = $_POST['username'];
   $em = $_POST['email'];
   $pswd = $_POST['password'];
   $pswd2 = $_POST['password2'];
   
   $s=$db->prepare("SELECT email FROM userss WHERE username=:username");
   $s->execute(array(':username'=>$un));

   $q=$db->prepare("SELECT username FROM userss WHERE username=:username");
   $q->execute(array(':username'=>$un));
   
  if( $fn == "" ) {
  $er='Enter your First name';
}
elseif( $ln == "" ) {
  $er='Enter your Last name';
}
elseif( $un == "" ) {
  $er='Enter your username';
}
elseif( $pswd == "" ) {
  $er='Enter your password';
}
elseif( strlen($pswd) < 6 ) {
  $er='Password must be more than 6 characters';
}
elseif( $pswd != $pswd2 ) {
  $er='Password and confirm password does not match!';
}
elseif( $em != "" && !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST["email"] ) ) {
  $er='Enter valid email';
}
elseif($q->rowCount() > 0) { 
  $er = 'The username '.$un.' is already taken!';
} 
elseif($s->rowCount() > 0) {
  $er='The email '.$em.' is already registered, choose another!';
}
else{
  $pswd = password_hash($pswd, PASSWORD_DEFAULT);
  $stmt = $db->prepare("INSERT INTO users (username,first_name,last_name,email,password) VALUES (:username,:first_name,:last_name,:email,:password)");  
$stmt->execute( array(':username'=>$un,':first_name'=>$fn,':last_name'=>$ln,':email'=>$em,':password'=>$pswd));
 }
if ($stmt->rowCount() == 1) {
  header("Location:login.php");
} 
else {  
echo "error";
 }
}
?>


HTML
<form action="register.php" method="POST">
    <fieldset>
        <input type="text" style="margin-top:5px;" name="fname" id="fn" required="required" maxlength="15" placeholder="First Name"/><br />
        <input type="text" name="lname" id="ln" required="required" maxlength="15" placeholder="Last Name"/><br />
        <input type="text" name="username" id="un" required="required" maxlength="15" placeholder="Username" class="username" /><br />
        <input type="email" name="email" id="em" required="required" maxlength="35" placeholder="Email"/> <br />
        <input type="password" name="password" id="pswd" required="required" maxlength="15" placeholder="Password"/><br />
        <input type="password" name="password2" id="pswd2" required="required" maxlength="15" placeholder="Confirm Password"/><br />
        <input type="submit" id="submit" name="reg" value="Create an Account">
        <div id="er"><?php echo $er; ?></div>
  </fieldset>
  </form>
Posted
Comments
Sergey Alexandrovich Kryukov 20-Nov-15 15:40pm    
Apparently, because you do nothing to show errors. Check up your last "if" condition.
Better yet, learn structural exception handling, instead of passing "errors". Maybe one simple fact can encourage you structural exception handling was invented around 1975; and the obsolete techniques of passing error condition was phased out in all non-nonsense software later, but also many years ago.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900