Why won't this snippet of code work?

65 Views Asked by At

I'm trying to set character limits for a register page and no matter what it says the username is too long can you guys tell me what's wrong? see the error for your self

    if(mb_strlen($uname) >= 20){
    if(mb_strlen($uname) <= 6){
    if(mb_strlen($pass1) <= 8){
    if(mb_strlen($pass1) >= 16){
    if((mb_strlen($email)) > 5 && strstr(($email1), "@") && strstr(($email1),".")){
        $pass = md5(md5("47Jdfio209".$pass1."4jfhioJasify3"));
        $pass = hash('sha512',$pass);


    $sql = $mysqli -> query("SELECT * FROM `archewor_users`.`users` WHERE `uname` = '$uname'");
    if(mysqli_num_rows($sql) > 0){
        echo"Sorry, that user already exists.";
    exit();

    }
    $mysqli -> query("INSERT INTO `archewor_users`.`users` (`id`, `name`, `lname`, `uname`, `email`, `pass`) VALUES (NULL, '$name', '$lname', '$uname', '$email1', '$pass')") or die(mysqli_error());
    }else{echo 'Your email must be valid.';}
    }else{echo 'This password is too long';}    
    }else{echo 'This password is too short';}
    }else{echo 'This username is too short';}

    }else{echo 'This username is too long';}

}else{
    echo "Sorry, your passwords do not match.<br />";   
    exit();
    }
}else{
    echo "Sorry, your emails do not match. <br /> <br />";
}




}else{
2

There are 2 best solutions below

1
On

Logics written by you is incorrect. Check your if conditions for both username and passwords validation. Written code has very poor standard. Use conditional operators instead of nested if conditions.

0
On

It's only a logical error

// 1. Here you check, if your username is to LONG
// So your -else- is called, if your username is to SHORT
if(mb_strlen($uname) >= 20){
  // all the rest
} else {echo 'This username is too long';} // This message should be "Username is to short"


Maybe better:

if(mb_strlen($uname) >= 20)
  die("Username is to long");

if(mb_strlen($uname) <= 6)
  die("Username is to short");