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.
PHP Code:
<!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
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:
PHP Code:
$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
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
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?
PHP Code:
<?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";
}
Bookmarks