This code is full of security holes and logical errors, but I have tried to rewrite it as best I can.
$user = 'root';
$host = 'localhost';
$password = '';
$database = 'online_examination';
// Attempt to connect to MySQL
if( !( $connection = mysql_connect( $host , $user , $password ) ) ){
  die( 'Failed to connect to server' );
}elseif( !( $db = mysql_select_db( $database , $connection ) ) ){
  die( 'Failed to connect to database' );
}
// Default values for Form Submitted Fields
$fn = $ln = $un = $pass = false;
// Check if Form Submitted
if( $_POST ){
  // For each value, perform some basic validation before trusting them
  if( isset( $_POST['fn'] ) && $_POST['fn']!='' )
    $fn = $_POST['fn'] ;    // firstname
  if( isset( $_POST['ln'] ) && $_POST['ln']!='' )
    $ln = $_POST['ln'] ;    // lastname
  if( isset( $_POST['un'] ) && $_POST['un']!='' )
    $un = $_POST['un'] ;  // username
  if( isset( $_POST['pw'] ) && $_POST['pw']!='' )
    $pass = $_POST['pw'] ;  // password
}
// If a Username was submitted
if( !$fn || !$ln || !$un || !$pw ){
  // One or more of the fields were empty or not submitted.
  // Show the form again (maybe with an error message)
}else{
  // Perform a Query looking for any instances where the same username is already in use
  $query = 'SELECT COUNT(*) AS matches FROM user_info WHERE username="'.mysql_real_escape_string( $un ).'"';
  $result = @mysql_query( $query , $connection ) ;
  if( !$result ){
    die( 'Query for Usernames Failed' );
  }
  $row = mysql_fetch_array( $result )
  if( $row['matches']!=0 ){
    // The Username is already in use
    if( !headers_sent() ){
      header( 'Location: /username_exists.php' );
    }else{
      echo 'Username already in use - <a href="/username_exists.php">Click here</a>';
    }
    die();
  }
  // If we have gotten to this point, the username is OK to use
  $sqlTpl = 'INSERT INTO user_info ( firstname , lastname , username , password ) VALUES ( "%s" ,  "%s" ,  "%s" , "%s" )';
  $sqlStr = sprintf( $sqlTpl ,
    mysql_real_escape_string( $fn ) ,
    mysql_real_escape_string( $ln ) ,
    mysql_real_escape_string( $un ) ,
    mysql_real_escape_string( $pw ) );
  $result = mysql_query( $sqlStr , $connection );
  if( $result ){
    if( !headers_sent() ){
      header( 'Location: /successfully_registered.php' );
    }else{
      echo 'Successfully registered - <a href="/successfully_registered.php">Click here</a>';
    }
    die();
  }else{
    // Something went wrong
  }
}
A few points from the hip:
- Looping through all returned rows and matching them individually is an obtuse way to check if a value exists. SQL is much better at doing that kind of thing - read up on it.
- Performing a check to see whether a password is already in use is pointless. I would bet that one or two people on StackOverflow have the same passwords, but they were not prompted with a message saying "Someone already has 'abc123' as a password. Pick another." If anything, that kind of message is a security risk rather than a security measure.
- Never trust input. Assuming that there will be a POST submission is a recipe for disaster.
- So is not validating whatever input you get.
- And even more so not escaping it for use within a database query. Google for "Little Bobby Tables".
- If you are going to store passwords, you should NEVER STORE THEM IN PLAIN TEXT. They should be hashed and salted. (Again, Google is your friend.)
- Assuming that you can change headers is something to be done with caution. Checking with headers_sent()is a good practice.
- Always test for errors as you go. A small error at the start, which could be detected and abort any following actions is better than letting a small mistake snowball.
Check out some of the pre-existing tutorials and/or PHP classes which handle user registrations. Alot of them have good ideas, which you should incorporate into your solutions rather than reinventing the wheel.