Zero767 Posted March 14, 2007 Share Posted March 14, 2007 Hey, im still new too php but anyway. I need to know how to check my database and see if the username someone is registering with is already taken. Here is my register code thus far: <?php $self = $_SERVER['PHP_SELF']; $firstname = $_POST ['firstname']; $username = $_POST ['username']; $password = $_POST ['password']; $email = $_POST ['email']; if( ( !$firstname ) or ( !$username ) or ( !$password ) or ( !$email ) ) { $form ="Please enter all of the details..."; $form.="<form action=\"$self\""; $form.="method=\"post\">First Name: "; $form.="<input type=\"text\" name=\"firstname\""; $form.=" value=\"$firstname\"><br />Username: "; $form.="<input type=\"text\" name=\"username\""; $form.=" value=\"$username\"><br />Password: "; $form.="<input type=\"password\" name=\"password\""; $form.=" value=\"$password\"><br />Email: "; $form.="<input type=\"text\" name=\"email\""; $form.=" value=\"$email\"><br />"; $form.="<input type=\"submit\" value=\"Submit\">"; $form.="</form>"; echo( $form ); } else { $conn = @mysql_connect( "localhost", "markeith_zero", "dbz123" ) or die ("Could not connect to MySQl database"); $db = @mysql_select_db ( "markeith_gamersodyssey", $conn ) or die ("Could not find database"); $sql = "insert into users (first_name,username,password,email) values (\"$firstname\",\"$username\",password(\"$password\"),\"$email\" )"; $result = @mysql_query( $sql, $conn ) or die("Could not execute query"); if( $result ) { echo( "Registration complete, welcome $username" ); mail("$email","Welcome to Gamers Odyssey!","-The Gamers Odyssey Team","Hey there, welcome to Gamers Odyssey! We just wanted to say thanks for joining "); } } ?> Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/ Share on other sites More sharing options...
trq Posted March 14, 2007 Share Posted March 14, 2007 <?php if ($result = mysql_query("SELECT username FROM users WHERE username = '{$_POST['username']}'")) { if (mysql_num_rows($result)) { // username is taken. } else { // username available. } } ?> Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206768 Share on other sites More sharing options...
Zero767 Posted March 14, 2007 Author Share Posted March 14, 2007 Thank you very much! I will give it a try now. Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206769 Share on other sites More sharing options...
Zero767 Posted March 14, 2007 Author Share Posted March 14, 2007 Well, I tried to put it in, but it says they dont exist even when they done and it still adds them. I am not sure where to put it, because if I put it soon in the code then it is before the result variable is even declaired. Sorry if this is a dumb question. Here is my updated code: <?php else { $conn = @mysql_connect( "localhost", "markeith_zero", "dbz123" ) or die ("Could not connect to MySQl database"); $db = @mysql_select_db ( "markeith_gamersodyssey", $conn ) or die ("Could not find database"); $sql = "insert into users (first_name,username,password,email) values (\"$firstname\",\"$username\",password(\"$password\"),\"$email\" )"; $result = @mysql_query( $sql, $conn ) or die("Could not execute query"); if ($result = mysql_query("SELECT username FROM users WHERE username = '{$_POST['username']}'")) { if (mysql_num_rows($result)) { // username is taken. echo ("Username is taken, please choose another"); } else { // username available. if( $result ) { echo( "Registration complete, welcome $username" ); mail("$email","Welcome to Gamers Odyssey!","-The Gamers Odyssey Team","Hey there, welcome to Gamers Odyssey! We just wanted to say thanks for joining "); } } } } ?> Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206776 Share on other sites More sharing options...
interpim Posted March 14, 2007 Share Posted March 14, 2007 This is how I do it... mysql_connect($server,$username,$password); mysql_select_db($database) or die("unable to select database"); $query="SELECT * FROM main WHERE user_name='$user'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); if ($num!=0) { echo "That username has already been taken, please try again<br> <a href='register.php'>Register</a>"; exit(); } Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206783 Share on other sites More sharing options...
mmarif4u Posted March 14, 2007 Share Posted March 14, 2007 Put this code before ur insert query. Try this: $sql = "SELECT COUNT(*) FROM users WHERE username = '{$_POST['username']}'"; $sql_result = mysql_query($sql); if (mysql_result($sql_result, 0) > 0) { Echo "This username is already in use.Please choose another."; } Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206785 Share on other sites More sharing options...
Zero767 Posted March 14, 2007 Author Share Posted March 14, 2007 Put this code before ur insert query. Try this: $sql = "SELECT COUNT(*) FROM users WHERE username = '{$_POST['username']}'"; $sql_result = mysql_query($sql); if (mysql_result($sql_result, 0) > 0) { Echo "This username is already in use.Please choose another."; } Says it is taken but still adds them to the database. Maybe if I add an else statment? Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206788 Share on other sites More sharing options...
interpim Posted March 14, 2007 Share Posted March 14, 2007 Add in something that will redirect the user back to the original form... then exit() the script... Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206789 Share on other sites More sharing options...
Zero767 Posted March 14, 2007 Author Share Posted March 14, 2007 Add in something that will redirect the user back to the original form... then exit() the script... lol, wow I didnt think of that at all. Works like a charm! Thanks Do you think you can help me set it up so the email must be valid? Thanks very much. Again i am having trouble figuring out where to place the code I got from the main site. Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206791 Share on other sites More sharing options...
mmarif4u Posted March 14, 2007 Share Posted March 14, 2007 Yes interpim is right add something to cll back the prev page agian after if username exists. To do it, its simple i put Echo, just put error like: error("This username is already in use.\\n Please choose another."); Make a another script with js and simply include it this page like: include('validation.php') Save this(validation.php) to the same directory. And this is validation.php code for u: <?php function error($msg) { ?> <script language="JavaScript"> <!-- alert("<?php echo ($msg) ?> "); history.back(); --> </script> <?php exit; } ?> Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206792 Share on other sites More sharing options...
mmarif4u Posted March 14, 2007 Share Posted March 14, 2007 Put ur email validation code before ur insert query. Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206793 Share on other sites More sharing options...
Zero767 Posted March 14, 2007 Author Share Posted March 14, 2007 Thanks so much, is there anyway I can I have it check if the email is unique at the same time as it checks for the usernames uniqueness? Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206796 Share on other sites More sharing options...
mmarif4u Posted March 14, 2007 Share Posted March 14, 2007 U can try the above code for email,just change ur variables there. Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206797 Share on other sites More sharing options...
Zero767 Posted March 14, 2007 Author Share Posted March 14, 2007 Yea, I got it to check them seperatly. Eh, thanks for everything, I will play with it till I get it. Thanks again. Link to comment https://forums.phpfreaks.com/topic/42607-check-if-username-is-taken/#findComment-206798 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.