-2

I am working on a project and I want to check the log in credential according to user. When a new user comes and it is log in, then if username present in database then produce an error !

username exist 

Code:

include('connect.php'); 

    $query = mysql_query("SELECT * FROM users WHERE user_name = '". $user_name ."' "); 

    if (mysql_num_rows($query) > 0) 
    { 
     echo 'Username already in use please try another.'; 
    }
    else{
    $opt=$obj->addStudent($user_name,$user_pass,$user_email,$user_contact);
    }
}

I want to check ignore case sensitive also.

6
  • ok...i read what u wrote...now... whats your question ??? Commented Mar 21, 2014 at 7:19
  • Read about SQL Injection also... - stackoverflow.com/questions/60174/… Commented Mar 21, 2014 at 7:19
  • 1
    also, mysql_ is history...read about mysqli_ or PDO Commented Mar 21, 2014 at 7:20
  • stackoverflow.com/questions/5629111/… this might help Commented Mar 21, 2014 at 7:20
  • convert case both sides before you test. Commented Mar 21, 2014 at 7:25

2 Answers 2

1
include('connect.php'); 

    $query = mysql_query("SELECT * FROM users WHERE UPPER(user_name) = '". mysql_real_escape_string(strtoupper($user_name)) ."' "); 

    if (mysql_num_rows($query) > 0) 
    { 
     echo 'Username already in use please try another.'; 
    }
    else{
    $opt=$obj->addStudent($user_name,$user_pass,$user_email,$user_contact);
    }
}

this way username :XyZ will be same as xyz

Sign up to request clarification or add additional context in comments.

4 Comments

logically this answer is correct...but your solution is immensely prone to sql injection ...be aware!! :)
i m 100% agree but i was just answering owner has to apply sanitation on string
@NoobEditor injuction part solved some how
OP will have to decide on what kind of security is needed in the code.... mysql_ is still insecure but yes does the job of answering OP's question!! :)
0

use the faster code for you unique name test.

include('connect.php'); 

    $query = mysql_query("SELECT `user_name` FROM users WHERE LOWER(user_name) = LOWER(". $user_name .")"); 

    if (mysql_num_rows($query) > 0) 
    { 
     echo 'Username already in use please try another.'; 
    }
    else{
    $opt=$obj->addStudent($user_name,$user_pass,$user_email,$user_contact);
    }

2 Comments

and how is this "faster"???
its mil-second difference for select * and select column name to whether match or not match.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.