Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Check on duplicate usernames or email address?

    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?
    PHP 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;
      }

    Thanks for you support!

  • #2
    New Coder
    Join Date
    Sep 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

    PHP Code:
    $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'] > )
    {
        if ( ( 
    $test['total_users'] ) <> )
        {
            echo 
    'there is already account with that name, using that email.<br />';
        }
        else
        {
            if ( ( 
    $test['total_users'] ) <> )
            {
                echo 
    'there is already account with that name.<br />';
            }

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

        exit ();
    }

    // no matches continue 

  • #3
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks will try it out.

    Cheers
    Thanks for you support!

  • #4
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Cool

    This solved it, thanks anyway.

    PHP Code:
    $username $_POST['username'];
    $Password $_POST['Password'];
    $Telephone $_POST['Telephone'];
    $Cellphone $_POST['Cellphone'];
    $status "OK";
    if ( 
    strlen($Password) < ){
    $msg1='<font face="Verdana" size="2" color="red">6 or more!</font>';
    $status"NOTOK";}
    if ( 
    strlen($username) < ){
    $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
    Last edited by westmatrix99; 06-01-2007 at 10:45 PM.
    Thanks for you support!

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,039
    Thanks
    19
    Thanked 42 Times in 42 Posts
    if($status<>"OK"){
    why not
    if($status != "OK"){

  • #6
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Thumbs up

    You are right, 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!
    Last edited by westmatrix99; 06-01-2007 at 10:50 PM.
    Thanks for you support!

  • #7
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Cool

    Quote Originally Posted by rafiki View Post
    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
    Thanks for you support!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •