I want PHP to make sure the username is not already used and also check to see if the field is empty. Sorry I am a huge noob when it comes to php. Here is my code:
// Check for an Username:
$dup = mysql_query("SELECT user_username FROM users WHERE user_username='".$_POST['user_username']."'");
if(mysql_num_rows($dup) >0){
$errors[] = 'Username already used.';
}
else{
$un = mysqli_real_escape_string($dbc, trim($_POST['user_username']));
echo '<b>Congrats, You are now Registered.</b>';
}
else {
$errors[] = 'You forgot to enter your Username.';
}
You have two
elsestatements, which cannot be used. Also, you need to escape your$_POSTdata. Lastly, you should move away from mysql_* functions as that time has passed. Use either mysqli or PDO.An alternative, and easier flow in my opinion, would be a try/catch statement. This way you don’t have to make an unnecessary database call if the username is empty: