Jump to content

check for duplicate username in input form


ibda12u

Recommended Posts

I'm going crazy with some code I've got. I have a signup form, that asks for a username.
I'm trying to build and if else statement that won't insert data into the table if the username is already in the table.

This is my code.
[code]
if($AcceptTerms=="" || $UserPlan=="" ){$errors=1; $error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
} [b]if[/b]($errors==1){ echo $error;}
$link = mysql_connect("localhost","data","passwd");
mysql_select_db("data",$link);
$query2="SELECT Username FROM signup where Username='".$Username."'";
$result2=mysql_query($query2) or die(mysql_error());
$num_rows=mysql_num_rows($result2);
[b]if [/b]($num_rows == 1)
{ echo 'Username in use, hit back button please';
}[b]else[/b] {
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Username: ".$Username."NewPassword1: ".$NewPassword1."NewPassword2:".$NewPassword2."
AcceptTerms: ".$AcceptTerms."UserPlan: ".$UserPlan." ";
$link = mysql_connect("localhost","data","passwd");
mysql_select_db("data",$link);
$query="insert into signup(Username,NewPassword1,NewPassword2,AcceptTerms,UserPlan) values ('".$Username."','".$NewPassword1."','".$NewPassword2."','".$AcceptTerms."','".$UserPlan."')";
mysql_query($query);
?>
<?php
}
?>[/code]

The code pretty much ignores my code to check the db for the username, and goes ahead and inserts the duplicate username and info in the DB.

Any idea's?
Link to comment
Share on other sites

if ($AcceptTerms=="" || $UserPlan=="" ){
$errors=1; $error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
}
if($errors==1){
echo "$error";
}
$link = mysql_connect("localhost","data","passwd");
mysql_select_db("data",$link);
$query2="SELECT Username FROM signup where Username='".$Username."'";
$result2=mysql_query($query2) or die(mysql_error());
$num_rows=mysql_num_rows($result2);
if($num_rows == 1){
echo 'Username in use, hit back button please';
}else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Username: ".$Username."NewPassword1: ".$NewPassword1."NewPassword2:".$NewPassword2."
AcceptTerms: ".$AcceptTerms."UserPlan: ".$UserPlan." ";
$link = mysql_connect("localhost","data","passwd");
mysql_select_db("data",$link);
$query="insert into signup(Username,NewPassword1,NewPassword2,AcceptTerms,UserPlan) values ('".$Username."','".$NewPassword1."','".$NewPassword2."','".$AcceptTerms."','".$UserPlan."')";
mysql_query($query);
}
?>


ive neatened it up a bit, to be honest, this whole code is a mess. so much unneeded. add me on my msn - joe@joeyjoe.co.uk , dont email as i cannot recieve emails at this time.
Link to comment
Share on other sites

[!--quoteo(post=353451:date=Mar 9 2006, 06:14 PM:name=joecooper)--][div class=\'quotetop\']QUOTE(joecooper @ Mar 9 2006, 06:14 PM) [snapback]353451[/snapback][/div][div class=\'quotemain\'][!--quotec--]
if ($AcceptTerms=="" || $UserPlan=="" ){
$errors=1; $error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
}
if($errors==1){
echo "$error";
}
$link = mysql_connect("localhost","data","passwd");
mysql_select_db("data",$link);
$query2="SELECT Username FROM signup where Username='".$Username."'";
$result2=mysql_query($query2) or die(mysql_error());
$num_rows=mysql_num_rows($result2);
if($num_rows == 1){
echo 'Username in use, hit back button please';
}else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Username: ".$Username."NewPassword1: ".$NewPassword1."NewPassword2:".$NewPassword2."
AcceptTerms: ".$AcceptTerms."UserPlan: ".$UserPlan." ";
$link = mysql_connect("localhost","data","passwd");
mysql_select_db("data",$link);
$query="insert into signup(Username,NewPassword1,NewPassword2,AcceptTerms,UserPlan) values ('".$Username."','".$NewPassword1."','".$NewPassword2."','".$AcceptTerms."','".$UserPlan."')";
mysql_query($query);
}
?>
ive neatened it up a bit, to be honest, this whole code is a mess. so much unneeded. add me on my msn - joe@joeyjoe.co.uk , dont email as i cannot recieve emails at this time.
[/quote]

Hmm... that still didn't resolve it. I added you as a buddy. msn - msanders@impact-x.com
It still just ignores the username I've inputted and adds it
Link to comment
Share on other sites

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.