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
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.