making sure a user name isn't used twice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ayfine
Forum Newbie
Posts: 22
Joined: Mon Nov 27, 2006 4:52 pm

making sure a user name isn't used twice

Post 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!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
ayfine
Forum Newbie
Posts: 22
Joined: Mon Nov 27, 2006 4:52 pm

Post 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);
        }
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
Post Reply