Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.