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 6 of 6
  1. #1
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts

    if user exist (make case sensitive)

    If you create a user, The code below works if the user name exist such as

    example username = "Matt" then Fail to make user name Matt,
    but if username = "matt" it will create another user named "matt" because its not case sensitive.

    PHP Code:
    $sql_query "SELECT * FROM users";$result=mysql_query($sql_query);
    while(
    $catch mysql_fetch_array($result)) 
    {
    $find_user_name $catch['user_name'];
    }
        
    if (
    $find_user_name == $user_name
    {
    $_SESSION['status'] = "Failed: User Already Exist!";
    header('Location: index.php?go=manage_users');
    exit;} 
    Thanks
    Last edited by sitNsmile; 02-06-2010 at 06:55 PM.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    For MySQL case sensitivity take a look here
    http://dev.mysql.com/doc/refman/5.0/...nsitivity.html

    Ideally you should search for the username in the query and check the number of returned results
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Actually just solved it...

    PHP Code:
    $duplicate_check="SELECT * FROM users WHERE user_name like '$user_name%'";
    $duplicate_result mysql_query($duplicate_check);
         if (
    mysql_num_rows($duplicate_result)>0)
        {
    $_SESSION['status'] = "Failed: User Already Exist!";
        
    header('Location: index.php?go=manage_users');
        exit;} 

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You don't want that trailing % with the username.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,855
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    $duplicate_check="SELECT * FROM users WHERE user_name like '$user_name%'";
    $duplicate_result = mysql_query($duplicate_check);
    if (mysql_num_rows($duplicate_result)>0)
    {$_SESSION['status'] = "Failed: User Already Exist!";
    header('Location: index.php?go=manage_users');
    exit;}
    Just add a unique key index to your user_name filed and then just insert the data into your table. In case of a duplicate, it'll throw you an error code and you may show a notice to the user, based on this.

    PS: Using SELECT * is very bad.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Although this is marked as resolved, I'll add one more comment for your consideration. In agreement with abduraooft, you're better off enforcing a UNIQUE constraint on your `user_name` field and then just perform the INSERT. If the INSERT statement fails with an error 1062, then backup a step and have them try a different user name. Overall, it's more efficient to perform a single INSERT and backtrace a step on only a few users than a SELECT and then INSERT for every user.

    Performing a SELECT * is bad practice, regardless of what you're looking for. In the case of performing a match, you'd usually return a SELECT COUNT(field) and perform some logic based on the count value. You don't need any actual data.

    Review this recent thread on essentially the same issue.


  •  

    Posting Permissions

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