title: Register form in PHP not working correctly
body: <p>I am having some trouble with my register form. It is registering new users perfectly, although the validation is causing me issues. When a user with the same username tries to register the validation picks this up perfectly, but i still get the diagloue box poping up confirming the user has successfully registered. I check the database when this happens and the user doesnt reigster as the validation has been carried out. How do I stop this happening?</p>
  
  <p>basically what i am wanting to achieve is for the message to stop saying 'successful' when a new user tries to enter a username which already exists, as the validation is picking this up 
  Here is my code</p>
  
  <pre><code>                &lt;?php
  
  
      if ($_POST["register"]=="yes") {
  
  
  //Declares the variables that will be posted to the database    
  $username = $_POST['username'];
  $first_name = $_POST['first_name'];
  $last_name = $_POST['last_name'];
  $email = $_POST['email'];
  $password = md5($_POST['password']);
  
  
  
  //Checks the database to see if username exists already
  $query = "SELECT * FROM users WHERE username = '$username'";
  $result = mysqli_query($db_connection, $query);
  $nums = mysqli_num_rows($result);
  
  //Checks the database to see if email address exists already
  $query2 = "SELECT * FROM users WHERE email = '$email'";
  $result2 = mysqli_query($db_connection, $query2);
  $nums2 = mysqli_num_rows($result2);
  
      if ($nums &gt;= 1)
      //informs user if username already exists
       echo "Username already exists, click &lt;a href = 'register1.php'&gt;HERE &lt;/a&gt; to try again";
      else if ($nums2 &gt;=1)
  
      //informs user if email already exists
      echo "Email Address already exists, click &lt;a href = 'register1.php'&gt;HERE &lt;/a&gt; to try again";
  
      else
      {
  
  $insert = 'INSERT INTO users(username, first_name, last_name, email, password,role_id, disable, activate) VALUES("'.$username.'","'.$first_name.'","'.$last_name.'","'.$email.'","'.$password.'", 2,0,0)';
  
  mysqli_query($db_connection, $insert);
    mysqli_close($db_connection);
             ("Location: mainpage.php");
  
  
  
    }
  
  
  {
    $i = 1;
  
  if ($i &gt; 0) {
      die;
  }
  else 
  
        "&lt;script&gt;alert (\"Registration Successful, your account has been sent for approval, and will be activated within 24 hours. \");&lt;/script&gt;";
         echo "&lt;meta http-equiv='refresh' content='0'&gt;";
       }
  
  
  
  
       }
  
  
  
  ?&gt;
  
  
  
  &lt;!-- Registration form --&gt;
  &lt;form name="register" id="register" method="post" action="register1.php"&gt;
  &lt;input type='hidden' name='register' value='yes'&gt;
  &lt;table border = '0'&gt;
  &lt;tr&gt;&lt;td&gt;&lt;strong&gt; First Name:&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;input type = "text" name = "first_name" size="30" value=""&gt;&lt;/td&gt;&lt;/tr&gt; 
  &lt;tr&gt;&lt;td&gt;&lt;strong&gt; Last Name:&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;input type = "text" name = "last_name" size="30" value=""&gt;&lt;/td&gt;&lt;/tr&gt; 
  &lt;tr&gt;&lt;td&gt;&lt;strong&gt; Username:&lt;/strong&gt;&lt;/td&gt;&lt;td&gt; &lt;input type = "text" name = "username" size="30" value=""&gt; &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&lt;strong&gt; Email: &lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;input type = "text" name = "email" size="30" value=""&gt; &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&lt;strong&gt; Password:&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;input type = "password" name = "password" size="30" value=""&gt; &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt; &lt;strong&gt;Verify Password:&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;input type = "password" name = "password2" size="30" value=""&gt; &lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;p&gt;
  &lt;input type ="button" value="Register" input class='button' onclick="verifyForm()"/&gt;
  &lt;/p&gt;
  &lt;/form&gt;
  </code></pre>