Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate username

Status
Not open for further replies.

columbo1977

Programmer
May 9, 2006
49
GB
Hi

Can anyone help. Attached is the registration form from a site I am building, the form is validated and the passwords are checked but I now need to check to make sure no-one enters the same username in the database.

Can anyone tell me how, as you can see from the function validuser() I have already been trying to do this.

Site has been built with PHP and MySQL.

Click on the link below for the registration form.


Thanks

Columbo1977
 
Do you understand that JavaScript runs on the browser and PHP on the server? And that because of this, you can't just call PHP code from within a PHP page like you can JavaScript?





Want the best answers? Ask the best questions! TANSTAAFL!
 
yes i understand, so have you any ideas on how i can check if the user that is registering is on the database already? and then ask the user to change the username?

cheers

Columbo1977
 
Typically, a user fills out a form on a static HTML page and submits that form to a PHP script. In a case such as you describe, where a user could take multiple times entering data and having the script return errors, I use a script that creates HTML that submits back to itself.

The PHP script takes the basic form of:

Code:
<?php
if (/* form was submitted */)
{
   //validate input
   if (/* input validated */)
   {
      //Use Location: header to send browser
      //next page
   }
   else
   {
      //display HTML with error and with form-
      //fields prepopulated with submitted values
   }
}
else
{
   //display blank form
}
?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
but how do i get it to check the sql database to see if the username has already been used?
 
use a call to the database

Code:
$q_result = mysql_query("select count(*) from usertable where username='".mysql_escape_string(trim($_POST['username']))."'");
$result = mysql_result ($q_result, 0, 0);
if ($result > 0)
{  echo "user name already in use"; }
else
{  echo "user name is free to use"; }
 
Unknown.

I would put it in my code at about the place where the comment:

//validate input

appears. After all, that's the code that validates the input, isn't it?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top