username verification
Moderator: General Moderators
-
fariquzeli
- Forum Contributor
- Posts: 144
- Joined: Mon Jun 24, 2002 9:16 am
- Location: Chicago
- Contact:
username verification
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?
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.......
}- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
There's a couple of mistakes in the above code, try this:
Obviously you'll need to edit this in order to take into account your own database structure and host, username and password etc.
Mac
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();
?>Mac
-
fariquzeli
- Forum Contributor
- Posts: 144
- Joined: Mon Jun 24, 2002 9:16 am
- Location: Chicago
- Contact:
tried, still having some problems.
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) {
$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");
}
}
?>-
fariquzeli
- Forum Contributor
- Posts: 144
- Joined: Mon Jun 24, 2002 9:16 am
- Location: Chicago
- Contact:
-
fariquzeli
- Forum Contributor
- Posts: 144
- Joined: Mon Jun 24, 2002 9:16 am
- Location: Chicago
- Contact:
-
fariquzeli
- Forum Contributor
- Posts: 144
- Joined: Mon Jun 24, 2002 9:16 am
- Location: Chicago
- Contact: