[SOLVED] Verify username

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kholloi
Forum Newbie
Posts: 19
Joined: Tue Mar 30, 2004 1:08 am

[SOLVED] Verify username

Post by kholloi »

HI Cool people

I am new to the whole PHP thing and need some help with a simple form. The form is used to input data into a customer database and email the results of the input to the HR dept.

The forms work well but I need to check whether a usename allready exists in the database, and if so either die or direct the user to the form for editing user data.

Here is what I came up with:

Admin Edit: Added tags and edited code (added spaces) so that it could wrap.][/size][/color]

Code: Select all

$link = mysql_connect("localhost","user1","*********");
mysql_select_db("dealerdb",$link);

$query = "SELECT *" .
"FROM client_info" .
"WHERE name = "$BusinessName";";
$result = mysql_query($query);
if ($result) {
die("Username taken");
} else {
$query="insert into client_info (name, industry, contact, position, oh_tel, ah_tel, fax, mobile, email, dob, interest, sale_status, sale_type, bike, car, cust_type) values ('".$BusinessName."', '".$Industry."', '".$ContactName."', '".$Position."', '".$TelNoOfficeHours."', '".$TelNoAfterHours."', '".$FaxNo."', '".$MobileNo."', '".$EmailAddress."', '".$DateOfBirth."', '".$MainInterest."', '".$SaleStatus."', '".$SaleType."', '".$BikeCustomer."', '".$CarCustomer."', '".$CustomerType."')";
mysql_query($query);

header("Refresh: 0;url=http://localhost/dealerdb");
}
?>
The script runs without any errors but it still allows duplicate entries in the name field.

Any sugestions?

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

Post by twigletmac »

Code and comments below:

Code: Select all

<?php

// add error handling to your database connection/selection calls so you
// can see decent errors if anything is going wrong
$link = mysql_connect('localhost', 'user1', '*********') or die(mysql_error());
mysql_select_db('dealerdb', $link) or die(mysql_error());

// * if you are checking to see whether someone is in a table, just select
//   one column, you don't need all of them
// * use single quotes around values in SQL statements, double quotes can
//   be used but singles don't need any escaping
$query = "SELECT name FROM client_info WHERE name = '$BusinessName'";

// add error handling to your mysql_query() call
$result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');

// when you do 
//    if ($result)
// you are not testing whether the username is taken, you are testing 
// whether the query returned true, you should instead test how many
// rows were returned:
if (mysql_num_rows($result) > 0) {
	echo '<p>The username has already been taken.</p>';
	exit();
} else {
	$query = "INSERT INTO client_info (name, industry, contact, position, oh_tel, ah_tel, fax, mobile, email, dob, interest, sale_status, sale_type, bike, car, cust_type) VALUES ('".$BusinessName."', '".$Industry."', '".$ContactName."', '".$Position."', '".$TelNoOfficeHours."', '".$TelNoAfterHours."', '".$FaxNo."', '".$MobileNo."', '".$EmailAddress."', '".$DateOfBirth."', '".$MainInterest."', '".$SaleStatus."', '".$SaleType."', '".$BikeCustomer."', '".$CarCustomer."', '".$CustomerType."')";

	mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');

	header('Refresh: 0;url=http://localhost/dealerdb');
}
?>
Mac
kholloi
Forum Newbie
Posts: 19
Joined: Tue Mar 30, 2004 1:08 am

Post by kholloi »

Thanks for the quick help.

It works fine now

Cheers
Post Reply