/    Sign up×
Community /Pin to ProfileBookmark

checking to see if a user name already exists

Hi, I am working on a login system and I am trying to see if a user name already exist, then the user would have to choose another user name.

I am not sure how to check the table and see. And yeah, I am a php newbie.?

Thanks in advance!

Here is my code.

[code=php]<!doctype html public “-//W3C//DTD HTML 4.0 //EN”>
<html>
<head>
<title>added</title>
</head>
<body>
<?php

$dbNewUser = $_REQUEST[‘newuser’]; // this gets the user name from the register.html form
$dbNewPassword = $_REQUEST[‘newpassword’]; // this gets the password from the register.html form

$dbh=mysql_connect (“localhost”, “hrpjeff_root”, “password”) or die (‘I cannot connect to the database because: ‘ . mysql_error()); // this connects to the database of dies
mysql_select_db (“hrpjeff_time”);

$query = “INSERT INTO users (id, name, password) VALUES ( 0, ‘$dbNewUser’, MD5( ‘$dbNewPassword’ ) )”; // this inserts the new user name and password into the table

$result = mysql_query($query) or die(“Query failed: ” . mysql_error()); // if it is not saved then a error is thrown

?>
</body>
</html>[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 19.2007 — Assign a unique index to the id column in the users table (or make it the primary key if applicable). Then when you do your query, check for a duplicate key error:
[code=php]
$result = mysql_query($query);
if(!$result)
{
if(mysql_errno() == 1062) // duplicate entry error
{
// output error message telling user to try another login ID
}
else
{
// some other error, so log it and display general error message to users
}
}
else
{
// new user account successfully created
}
[/code]
Copy linkTweet thisAlerts:
@graphical_forceauthorDec 19.2007 — Here is what I have tried so far. I left the errors for testing purposes. I also changed the id to the primary key. I still can create more than one account with the same user name.

I would think that I need to check this before writing it to the table.

Thanks for the help so far. Any other suggestions?

[code=php]<?php

$dbNewUser = $_REQUEST['newuser']; // this gets the user name from the register.html form
$dbNewPassword = $_REQUEST['newpassword']; // this gets the password from the register.html form

$dbh=mysql_connect ("localhost", "hrpjeff_root", "password") or die ('I cannot connect to the database because: ' . mysql_error()); // this connects to the database of dies
mysql_select_db ("hrpjeff_time");

$query = "INSERT INTO users (id, name, password) VALUES ( 0, '$dbNewUser', MD5( '$dbNewPassword' ) )"; // this inserts the new user name and password into the table

//$result = mysql_query($query) or die("Query failed: " . mysql_error()); // if it is not saved then a error is thrown

//header( "Location: index.php" ); // redirect to the login if registration is successful.

$result = mysql_query($query);
if(!$result)
{
if(mysql_errno() == 1062) // duplicate entry error
{
echo " // output error message telling user to try another login ID";
}
else
{
echo "// some other error, so log it and display general error message to users";
}
}
else
{
echo "// new user account successfully created";
}

?>[/code]
Copy linkTweet thisAlerts:
@graphical_forceauthorDec 19.2007 — Thanks for the help. I seem to have figured it out myself!

Now on to the next problem......
×

Success!

Help @graphical_force spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...