Page 1 of 1

username verification

Posted: Mon Jun 24, 2002 9:16 am
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?

try this?

Posted: Mon Jun 24, 2002 9:59 am
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.......

}

Posted: Mon Jun 24, 2002 10:07 am
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

tried, still having some problems.

Posted: Mon Jun 24, 2002 10:24 am
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;
  
?>

Posted: Mon Jun 24, 2002 10:25 am
by fariquzeli
sorry just posted before that previous post appeared, let me try that. thanks.

Posted: Mon Jun 24, 2002 10:28 am
by fariquzeli
just wondering...


I'm a newbie, what's the ! points for before equal signs?

Posted: Mon Jun 24, 2002 10:32 am
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'

Posted: Mon Jun 24, 2002 10:38 am
by fariquzeli
yes thanks, one more thing, can someone explain this line in the above code:

$user = $_POST['user'];

What's that doing?

Posted: Mon Jun 24, 2002 3:21 pm
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.