You should use two methods to safe-guard against duplicate usernames -
First - the username column of your db should have a UNIQUE KEY on it.
Second - check for the existance of the new username
$sql = "SELECT * FROM tbl_name WHERE username='$username'";
$result = mysql_query($sql);
if ($result) {echo 'Some error msg';}
else {/*insert code goes here*/}
Hope this helps 😉