Jump to content

[SOLVED] Basic Check if 'Username" already exists...


suttercain

Recommended Posts

Hi guys, I have

<?php
$usertest = mysql_query("SELECT username FROM merchants WHERE username = '" .mysql_real_escape_string($_POST['username']). "'");
if ($usertest) {
echo $_POST['username']. " is already taken. Please select a different username.";
} else {
//process other code
}
?>

What am I doing wrong. I need to check if the username is already in the database. If it is, the script stops. If not, the code process begins.

 

Thanks.

You need to pull the data

 

<?php
$usertest = mysql_query("SELECT username FROM merchants WHERE username = '" .mysql_real_escape_string($_POST['username']). "'");
$num_rows = mysql_num_rows($usertest);
if ($num_rows >0) {
echo $_POST['username']. " is already taken. Please select a different username.";
} else {
//process other code
}
?>

 

<?php
$usertest = mysql_query("SELECT username FROM merchants WHERE username = '" .mysql_real_escape_string($_POST['username']). "'");
$user_exist= mysql_num_rows($usertest);
if ($user_exist > 1) {
echo $_POST['username']. " is already taken. Please select a different username.";
            exit(); // Exit the script
} else {
//process other code
}
?>

 

Got there before me lol

 

If you want to quit the script you will need to put "exit();" on line 5 as i've done.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.