Jump to content

registration check username


MDanz

Recommended Posts

i'd like to check if the username is taken. ........if it is it echo's " bla bla choose another name" and also for email, so no duplicate email for users.  how do i do this?

 

 

here's the part of the code thats relevant


     echo "<h1>Register</h1>";
         
         $submit = $_POST['submit'];
         
         $fullname = strip_tags($_POST['fullname']);
         $username = strip_tags($_POST['username']);
         $password = strip_tags($_POST['password']);
         $email = $_POST['email'];
         $confirmpassword = strip_tags($_POST['confirmpassword']);
         $date = date("Y-m-d");

         if($submit)
         {
           
           //check fields are filled
           
           if($fullname&&$username&&$password&&$confirmpassword)
           {

              
              if ($password==$confirmpassword)
              {
               //check character length of username and fullname
               
               if(strlen($username)>25||strlen($fullname)>25)

               {
                echo" Length of username or fullname is too long";
               }

               else {
                //check password length
                
                if(strlen($password)>25||strlen($password)<6)
                {
                  echo "Password must be between 6 and 25 characters";
                }
                else
                {
                 //register the user
                 //encryt password
              $password = md5($password);
              $confirmpassword = md5($confirmpassword);
              
              //open database
              
              $connect =mysql_connect("localhost","Master","password");
              mysql_select_db("Login");  //select database
                //generate random number for activation process
                
                $random = rand(23456789,98765432);
              $queryreg = mysql_query("  
              INSERT INTO Users VALUES ('','$fullname','$username','$password','$email','$date','$random','0','')
                                           ");
              $lastid = mysql_insert_id();


             

Link to comment
https://forums.phpfreaks.com/topic/171718-registration-check-username/
Share on other sites

This is going to be psuedo code, so you'll need to adapt it a bit

 

As far as mailing, take a look at this: http://us3.php.net/manual/en/function.mail.php

 


              $connect =mysql_connect("localhost","Master","password");
              mysql_select_db("Login");  //select database
                //generate random number for activation process

              $sql = "SELECT * FROM `Users` WHERE user='$fullname'";
	$result = mysql_query($sql) or die(mysql_error());
	if (mysql_fetch_row($result) > 0)
	{
		echo "Username in use, please select another";
	}
	else
	{
                	$random = rand(23456789,98765432);
                $queryreg = mysql_query(" 
        	        INSERT INTO Users VALUES ('','$fullname','$username','$password','$email','$date','$random','0','')
                                           ");
                	$lastid = mysql_insert_id();
	}

use mysql_num_rows to check whether email or username has been taken by other users

 

$username=$_POST['username'];

$query="SELECT * FROM yourdatabasetable WHERE usernamecolumn='$username";

 

connect this query and use mysql_num_rows

 

$count = mysql_num_rows($query)

if($count==1){echo "Username has been used";}else{

you can insert it into your database

 

hope it could help

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.