Jump to content

Check if username is taken.


Zero767

Recommended Posts

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

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 "); }
    }
  }
}
?>

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();
  }

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.";
}

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?

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  :D

 

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.