Page 1 of 1

making sure a user name isn't used twice

Posted: Mon Nov 27, 2006 5:01 pm
by ayfine
I'm working on a site for a my friends and I to post our homework. So far I have a lot of it done but I need to make sure a username isn't used twice. How would you go about doing this?
This is what I have so far for the user registration function.

Code: Select all

function register($user, $pass, $email, $email_redo) {
        	if($email != $email_redo) {
                	header("location: {$page['register']}?msg=emailmatch");
                }
                $sql = mysql_query("Insert into users (user, pass, email) VALUES('$user', '$pass', '$email')") or die(mysql_error());
                $msg = "$user, thank you for siging up on {$site['url']} . \n Your login information is \n user: $user \n password: $pass";
                $msg2 = wordwrap($msg, 70);
                mail($email, "Your Login information for the hw site", $msg2);
        }
thanks!

Posted: Mon Nov 27, 2006 5:03 pm
by Burrito
select off of the database first to make sure the name isn't in there. If the row count is > 0, display an error message. Also, make your field unique in mysql so it won't allow the insert to happen if it gets past that point somehow.

Posted: Mon Nov 27, 2006 5:09 pm
by ayfine
Burrito wrote:select off of the database first to make sure the name isn't in there. If the row count is > 0, display an error message. Also, make your field unique in mysql so it won't allow the insert to happen if it gets past that point somehow.
so this would do it?

Code: Select all

function register($user, $pass, $email, $email_redo) {
        	if($email != $email_redo) {
                	header("location: {$page['register']}?msg=emailmatch");
                }
                $check = mysql_query("select * from users") or die(mysql_error());
                $rows = mysql_num_rows($check);
                if($rows > 0) {
                        die("There is someone with the same account name already");
                }
                $sql = mysql_query("Insert into users (user, pass, email) VALUES('$user', '$pass', '$email')") or die(mysql_error());
                $msg = "$user, thank you for siging up on {$site['url']} . \n Your login information is \n user: $user \n password: $pass";
                $msg2 = wordwrap($msg, 70);
                mail($email, "Your Login information for the hw site", $msg2);
        }

Posted: Mon Nov 27, 2006 5:14 pm
by Burrito
well you need to select where the username is the username in question, otherwise it's going to return rows if any exist on the table at all.