I am using a register script but by some reason this script is not working.
First of all here is my <html> form:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
 <legend>Member Registration</legend>
 <p><label>Username:</label><input name="username" type="text" maxlength="20" <?php if(isset($error)) {echo "value='$username'";} ?> /></p>
 <p><label>Password:</label><input name="password" type="password" maxlength="20" /></p>
 <p><label>Confirm Password:</label><input name="password2" type="password" maxlength="20" /></p>
 <p><label>Email:</label><input name="email" type="text" maxlength="255" <?php if(isset($error)) {echo "value='$email'";} ?> /></p>
 <p><input type="submit" name="submit" value="Register"></p>
</form>
After clicking on the submit button the script needs to get posted. Before adding the values to the database the php script should do a check:
if (strlen($username) < 3){
 $error[] = 'User name must be between 3 and 20 characters.';
}
When I enter just 1 character also this is not checked. When I click on the submit button the script returns into its first state.
Why is this happening? I have set the reporting of errors on, but also when I do that I dont get any error message.
How can I fix this problem?
Here is my full PHP code:
<?php
 $dbhost = 'localhost';
 $dbuser = 'root';
 $dbpass = '';
 $dbname = 'db';
 $conn = mysqli_connect ($dbhost, $dbuser, $dbpass);
 $conn = mysqli_select_db ($conn, $dbname);
 if(!$conn){
  die( "Sorry! There seems to be a problem connecting to our database. Please give us a few minutes to remedy the problem. Thank you.");
 }
 function errors($error){
  if (!empty($error))
  {
   $i = 0;
   while ($i < count ($error)){
    echo '<span class="warning">'.$error[$i].'</span>';
    $i ++;
    }
   }
   if (isset($_POST['submit'])){
    $username = trim($_POST['username']);
    if (strlen($username) < 3){
     $error[] = 'User name must be between 3 and 20 charactors.';
    }
    if(!get_magic_quotes_gpc()){
     $POST[] = addslashes($_POST['username']);
    }
    $usercheck = $_POST['username'];
    $check = mysqli_query($conn, "SELECT username FROM users WHERE username ='".$usercheck."'")or die(mysqli_error());
    $check2 = mysqli_num_rows($check);
    if ($check2 != 0) {
     $error[] = 'Sorry, the username <b>'.$_POST['username'].'</b> is already in use.';
    }
    $password = trim($_POST['password']);
    if (strlen($password) < 5) {
     $error[] = 'password Must be between 5 and 20 characters.';
    }
    if ($_POST['password'] != $_POST['password2']) {
     $error[] = 'Your passwords did not match.';
    }
    if (!get_magic_quotes_gpc()) {
     $_POST[] = addslashes($_POST['email']);
    }
    $emailcheck = $_POST['email'];
    $hash = md5( rand(0,1000) ); 
    $emailcheck1 = mysqli_query($conn, "SELECT email FROM members WHERE email = '".$emailcheck."'")or die(mysqli_error());
    $emailcheck2 = mysqli_num_rows($emailcheck1);
    if ($emailcheck2 != 0) {
     $error[] = 'Sorry, the email address <b>'.$_POST['email'].'</b> is already in use, Please choose another email address.';
    }
    if (!$error ) {
     $username = $_POST['username'];
     $password = $_POST['password'];
     $email = $_POST['email'];
     if(!get_magic_quotes_gpc())
     {
      $username = addslashes($username);
      $password = addslashes($password);
      $email = addslashes($email);
     }
     $username = mysqli_real_escape_string($username);
     $password = mysqli_real_escape_string($password);
     $email = mysqli_real_escape_string($email);
     $username = strip_tags($username);
     $password = strip_tags($password);
     $email = strip_tags($email);
     $username = ucwords(strtolower($username));
     $email = strtolower($email);
     $insert1 = "INSERT INTO members (username, password, email) VALUES ('$username', md5('$password'), '$email')";
     $result1 = mysqli_query($insert1) or die('Error : ' . mysqli_error());
    }
   }
  }
?>
