username verification
Posted: Mon Jun 24, 2002 9:16 am
What's the best way to verify a username against a MySQL database to see if the name is already in use or not?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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.......
}Code: Select all
<?php
$dbconn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$user = $_POSTї'user'];
$sql = "SELECT username FROM userinfo WHERE username = '$user'";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
echo 'Username taken';
} else {
......place your "add user" script part here.......
}
mysql_close();
?>Code: Select all
<? //initilize PHP
include("webvars.inc");
mysql_connect("$hostname","$user","$pass") or die(); //connect
mysql_select_db("pancorp"); //select db
if($submit) {
$user_check = "SELECT username FROM technicians WHERE username = '$username'";
$user_result = mysql_fetch_assoc( $user_check );
if ( !$user_result ) {
echo 'The username you have entered is taken, please choose another username.';
}
else {
$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");
}
}
?>