View Full Version : username and email exist in db?
I have the following code to check if user already exist in mysql database :
$find_username = mysql_query("SELECT username FROM register WHERE username = '".varCheck($_POST['username'])."'");
$duplicate_username = mysql_num_rows($find_username);
varCheck is a function to remove HTML tags, etc.. is not essential now!
if ( ($duplicate_username == 0 ) {
// user added
} else {
// user already exist
}
Ok, this thing work. I get an error if user already exist in database.
I want to do the same thing to check if E-mail exist. I need to write a separate mysql_query and if function for email ?
$find_email = mysql_query("SELECT email FROM register WHERE email = '".varCheck($_POST['email'])."'");
$duplicate_email = mysql_num_rows($find_email);
if ( ($duplicate_email == 0 ) {
// user added
} else {
// email exist
}
Exist a simpler way to do this ?
Hi zoliky,
I have the following code to check if user already exist in mysql database :
$find_username = mysql_query("SELECT username FROM register WHERE username = '".varCheck($_POST['username'])."'");
$duplicate_username = mysql_num_rows($find_username);
if ( ($duplicate_username == 0 ) {
// user added
} else {
// user already exist
}
...
I want to do the same thing to check if E-mail exist. I need to write a separate mysql_query and if function for email ?
If I understand you correctly, you want to check if a username /email combination is already taken.
I would combine the two checks in one:
$find_username = mysql_query("SELECT email, username FROM register WHERE email = $email OR username = '".varCheck($_POST['username'])."'");
}
then I would not just count the rows, but I would access the data to see if its the email or the username that is duplicate.
I would also put UNIQUE-Constraints on email and username, which means you will get at most 2 rows (if both email and username are taken - by different people)
Cheers!
ok what duesi means is:
$error=false;
$fetch_exist=mysql_query("SELECT email, username FROM register WHERE email = $email OR username = '".varCheck($_POST['username'])."'");
//then check if dupplicate
if(mysql_num_rows($fetch_exist)>0) {
$rst=mysql_fetch_array($fetch_exist);
if ( ($rst["username"] == $_POST['username'] ) {
$error=true;
$username=""; //the code to reset textbox from previous thread you make
}else{
$username=$_POST['username'];
}
if ( ($rst["email"] == $_POST['email'] ) {
$error=true;
}
}
//then after checking error add
if($error==false) {
//mysql_query 'INSERT....' here
}
No, I am afraid this will not work, because in the worst case, you will have 2 rows.
If you have such data:
username | email
-----------------
zoliky | nobody@here.com
me | some@mail.com
if someone now tries to register as "me" using "nobody@here.com",
the query will return exactly the two rows you see above.
But apart from this, I agree with your code.
Here is how I would do it, building up on your code:
$fetch_exist=mysql_query("SELECT email, username FROM register WHERE email = '".varCheck($_POST['email'])."'" OR username = '".varCheck($_POST['username'])."'");
//then check if dupplicate
if(mysql_num_rows($fetch_exist)>0)
{
while ($rst = mysql_fetch_array($fetch_exist, MYSQL)) {
//Is it the username or the email?
if ( $rst["username"] == $_POST['username'] )
{
//its the username
$error_message .="Your Username is already taken. ";
}
elseif ( $rst["email"] == $_POST['email'] )
{
//its the email
$error_message .="Your EMail is already taken. ";
}
}
}
else
{
//OK, everything is fine
//Otherwise we would have gotten any rows (thus $error is not needed)
//mysql_query 'INSERT....' here
}
//Now display $error_message and give the user another chance
Happy Coding!
yeah you are right...I miss the "OR" statement in mysql
sorry about that
By the way: you should always assume that you get more than one row.
If there is no Unique Key and you say
WHERE username='whatever'
you can get 0,1 or many rows. Code should never ever depend on assumptions (the mother of all <<snip>>)...
Cheers!
vBulletin® v3.6.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.