...

View Full Version : Checking for duplicate records in database



xiaodao
01-08-2006, 08:21 PM
i got a form


<form method="post" action="haha.php">
username<input type="text" name="username">
<br>
email<input type="text" name="username">
<br>
<input type="submit" value="submit">
</form>

now i want to add a button beside the username field to check whether the user name is duplicated in database, what to d? please give a suggestion, thanks

Element
01-08-2006, 08:38 PM
I think this is beyond just PHP, the besy way to do it is to submit and if the name exists in the database you would echo out the same form with the $_POST information they submitted and put a note next to the name field and have them change it.

The easiest way to check if a user exists is like this:




if(mysql_query("SELECT COUNT(id) FROM users WHERE username=`".$_POST['username']."`")) {
$user_exists = true;
}



then wherever you want to echo the form if the user exists you check if $user_exists is true and then if so echo the form again. You could even do it right in that if statement above.

xiaodao
01-08-2006, 08:44 PM
Hi, thanks, i used to do your method, but i got see this kind of function in quite a lot of sites and i feel want to know the technology behind

Element
01-08-2006, 08:49 PM
I'm sure its AJAX and PHP. I havn't used AJAX besides Velox example so I don't think I could successfully help you there.

marek_mar
01-08-2006, 11:19 PM
if(mysql_query("SELECT COUNT(id) FROM users WHERE username=`".$_POST['username']."`")) {
$user_exists = true;
}



That won't work becouse an mysql_query returns true regardless of the result set being empty or not... in fact it will always return something. COUNT(id) will be just equal 0. Moreover the query won't work as you used backticks instead of quotes.

Spookster
01-08-2006, 11:37 PM
Your thread title is vague. Please read our posting guidelines
http://www.codingforums.com/postguide.htm

xiaodao
01-09-2006, 01:14 AM
sorry, i tried to change the title, but cannot



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum