username verification

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

username verification

Post by fariquzeli »

What's the best way to verify a username against a MySQL database to see if the name is already in use or not?
sneakysockies
Forum Newbie
Posts: 4
Joined: Fri Jun 21, 2002 11:47 pm
Location: hollister, CA
Contact:

try this?

Post by sneakysockies »

well.... i guess you could go (ill use $un as your form username field)

Code: Select all

$un_chek = "SELECT username FROM userinfo WHERE username = '$un'";

$un_result = mysql_fetch_assoc( $un_chek );

if ( !$un_result ) {

echo 'Username taken';

} else {

......place your "add user" script part here.......

}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There's a couple of mistakes in the above code, try this:

Code: Select all

<?php
$dbconn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$user = $_POST&#1111;'user'];
$sql = "SELECT username FROM userinfo WHERE username = '$user'";
$result = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result) != 0) &#123; 
    echo 'Username taken'; 
&#125; else &#123; 
    ......place your "add user" script part here....... 
&#125;

mysql_close();
?>
Obviously you'll need to edit this in order to take into account your own database structure and host, username and password etc.

Mac
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

tried, still having some problems.

Post by fariquzeli »

I want to have the username verified against the database. The form echo's itself, yet when i test it on my server and i hit the submit button, it just seems to refresh the form page without showing the thank you page I have created, or the error message I have. Can anyone help? Here is the code I have:

Code: Select all

<? //initilize PHP
  include("webvars.inc");
  mysql_connect("$hostname","$user","$pass") or die(); //connect
  mysql_select_db("pancorp"); //select db
  
  if($submit) &#123;
  
  	$user_check = "SELECT username FROM technicians WHERE username = '$username'"; 

	$user_result = mysql_fetch_assoc( $user_check ); 

	if ( !$user_result ) &#123; 

	echo 'The username you have entered is taken, please choose another username.'; 
	&#125; 
	else &#123; 

 $sql = "INSERT INTO technicians (id,username,password,firstname,lastname,email,location,phonenumber)".
  "VALUES ('NULL', '$username', '$password', '$firstname', '$lastname', '$email', '$location', '$phonenumber')";
  $result=mysql_query($sql) or die('Username in use'); //Insert into db
  header ("location: http://www.pancorp.com/techs/thanks.php");
		&#125; 
&#125;
  
?>
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

sorry just posted before that previous post appeared, let me try that. thanks.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

just wondering...


I'm a newbie, what's the ! points for before equal signs?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

i assume you're talking about what != means

! is the equivalent of 'NOT', so if put before an = sign, then != means 'NOT EQUAL'
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

yes thanks, one more thing, can someone explain this line in the above code:

$user = $_POST['user'];

What's that doing?
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

$user = $_POST['user']; is for when a person has register_globals turned on, see the sticky in the PHP sub forum for more info.
Post Reply