...

View Full Version : Resolved if user exist (make case sensitive)



sitNsmile
02-06-2010, 06:46 PM
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.



$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

JAY6390
02-06-2010, 06:50 PM
For MySQL case sensitivity take a look here
http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

Ideally you should search for the username in the query and check the number of returned results

sitNsmile
02-06-2010, 06:54 PM
Actually just solved it...



$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;}

kbluhm
02-06-2010, 08:00 PM
You don't want that trailing % with the username.

abduraooft
02-07-2010, 03:52 PM
$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.

bdl
02-07-2010, 04:08 PM
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 (http://www.codingforums.com/showthread.php?t=186925) on essentially the same issue.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum