...

View Full Version : Check on duplicate usernames or email address?



westmatrix99
05-30-2007, 09:34 AM
I have created a server behaviour in Dreamweaver to check if an email exists when someone registers at the site but I need to check the username as well.
Any ideas on how to modify this code?

// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
$MM_dupKeyRedirect="userexists.php";
$loginUsername = $_POST['email'];
$LoginRS__query = sprintf("SELECT Email FROM tblogin WHERE Email=%s", GetSQLValueString($loginUsername, "text"));
mysql_select_db($database_cnmobile, $cnmobile);
$LoginRS=mysql_query($LoginRS__query, $cnmobile) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);

//if there is a row in the database, the username was found - can not add the requested username
if($loginFoundUser){
$MM_qsChar = "?";
//append the username to the redirect page
if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
$MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
header ("Location: $MM_dupKeyRedirect");
exit;
}
}

printf
05-30-2007, 12:10 PM
Just create a condition in your query, so it returns a bitwise number so you can figure what exactly is wrong. If you don't care to know the exact problem (user exists or/and email exists), then just COUNT(user) and COUNT(email) if they return greater than 0 then a match was found in either (user, email). I would use the bitwise method so I know exactly what matched or didn't match using a single query!


$user = 'user_name_to_check';
$email = 'email_address_to_check';

$user = mysql_real_escape_string ( $user );
$email = mysql_real_escape_string ( $email );

$query = "SELECT SUM(IF(user='" . $user . "' && email='" . $email . "',1,IF(user='" . $user . "' && email!='" . $email . "',2,IF(user!='" . $user . "' && email='" . $email . "',4,0)))) AS total_users FROM tblogin WHERE user = '" . $user . "' OR email = '" . $email . "';";

$res = mysql_query ( $query );

$test = mysql_fetch_assoc ( $res );

if ( $test['total_users'] > 0 )
{
if ( ( 1 & $test['total_users'] ) <> 0 )
{
echo 'there is already account with that name, using that email.<br />';
}
else
{
if ( ( 2 & $test['total_users'] ) <> 0 )
{
echo 'there is already account with that name.<br />';
}

if ( ( 4 & $test['total_users'] ) <> 0 )
{
echo 'that email has been used to register another account.<br />';
}
}

exit ();
}

// no matches continue

westmatrix99
05-30-2007, 12:56 PM
Thanks will try it out.

Cheers

westmatrix99
06-01-2007, 03:45 PM
This solved it, thanks anyway.



$username = $_POST['username'];
$Password = $_POST['Password'];
$Telephone = $_POST['Telephone'];
$Cellphone = $_POST['Cellphone'];
$status = "OK";
if ( strlen($Password) < 6 ){
$msg1='<font face="Verdana" size="2" color="red">6 or more!</font>';
$status= "NOTOK";}
if ( strlen($username) < 6 ){
$msg2='<font face="Verdana" size="2" color="red">6 or more!</font>';
$status= "NOTOK";}
if ( strlen($Telephone) < 10 ){
$msg4='<font face="Verdana" size="2" color="red">Failed!</font>';
$status= "NOTOK";}
if ( strlen($Cellphone) < 10 ){
$msg5='<font face="Verdana" size="2" color="red">Failed!</font';
$status= "NOTOK";}
$quser = "SELECT username FROM tblogin WHERE username = '".$username."'";
$result = mysql_query($quser);
if(mysql_affected_rows()==1){
$msg3='<font face="Verdana" size="2" color="red">Already exists!</font>';
$status= "NOTOK";}
if($status<>"OK"){
} else {


Cheers

rafiki
06-01-2007, 10:24 PM
if($status<>"OK"){
why not
if($status != "OK"){

westmatrix99
06-01-2007, 10:45 PM
You are right,:thumbsup: I did not really bother to look at options in the code.

I was happy just to find that code that I had for over a year and didn't bother with it till now.
I can change it as I also realized that you don't have to declare variables in PHP, Yipeeeee!:D

westmatrix99
06-01-2007, 10:48 PM
if($status<>"OK"){
why not
if($status != "OK"){

You know rafiki, I believe that if you know it, show it.

I have no problems with laying out the code I find/found or built as that was the whole idea of PHP, Linux and the rest.

Cheers



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum