Hi
I am using php5 and Mysql
I want to generate user id like phone format (Ex: xxx-xxx-xxxx). and check that user id exists in database or not. if yes, i have to generate new user id etc. If that user id not in database, i have to insert user id for new user. Means i have generate unique user id for login purpose.
Generating user id is working fine. availability of user id also ok for me by using mysql_num_rows() function. But if user id i exists in database, how can i generate new user id. All this will be done in single instance (When user click on submit button from registration form).
Here is my script.
<?php
function UserID()
{
$starting_digit=5;
$first_part1=rand(0,99);
if(strlen($first_part1)==1) { $first_part="0".$first_part1; } else { $first_part=$first_part1; }
$second_part1=rand(1,999);
if(strlen($second_part1)==1) { $second_part="00".$second_part1; } elseif(strlen($second_part1)==2) { $second_part="0".$second_part1; } else { $second_part=$second_part1; }
$third_part1=rand(1,9999);
if(strlen($third_part1)==1) { $third_part="000".$third_part1; } elseif(strlen($third_part1)==2) { $third_part="00".$third_part1; } elseif(strlen($third_part1)==3) { $third_part="0".$third_part1; } else { $third_part=$third_part1; }
$userid=$starting_digit.$first_part."-".$second_part."-".$third_part;
return $userid;
}
$random_userid=UserID();
$sql=mysql_query("select * from users where user_id='".$random_userid."'");
if(mysql_num_rows($sql)==0)
{
$insert=mysql_query("insert into users (user_id) values ('".$random_userid."')");
}
?>
If user id not exists in database, that user id inserting in database successfully. If not, empty value is inserting, not generating new user id.
Can any one help me out please.