Jump to content

[SOLVED] Checking if Username Already Exists in the DB


Recommended Posts

Hey Guys,

 

I am trying to fix this script:

<?php 
if (isset($_POST['submit']))
{
if($_POST['password'] == $_POST['confirmpassword'])
{
	$password = 'Ok';
	$dbhost = "localhost";
	$dbuser = "username";
	$dbpass = "pass";
	$dbname = "game";
	$connect = mysql_connect($dbhost,$dbuser,$dbpass);
	if (!$connect)
	{
		die('MySQL Error ' . mysql_error());
	}
	else
	{
	$selectdb =	mysql_select_db($dbname);
	if (!$selectdb)
	{
		die('MySQL Error ' . mysql_error());
	}
	else
	{
	$checkuser = mysql_query("SELECT * FROM accounts WHERE username = '$username'");

	if (!$checkuser)
	{
		die('MySQL Error' . mysql_error());
	}		
	else
	{

	if(mysql_num_rows($checkuser) > '1')
		{
			$error = '<font color="red"> User Already Exists </font>';
		}
	else
	{
		$username = $_POST['username'];
		$password = $_POST['password'];
		$password = md5($password);
	$adduser = mysql_query("INSERT INTO accounts (username, password, last_login) VALUES ('$username' , '$password' , '0')");
	if (!$adduser)
	{
		die('MySQL Error ' . mysql_error());
	}
	else
	{
	$error = '<font color="green">User Added Succesfully </font>';
	}		
	}		
	}
}
}
}
else
{
$error = '<font color="red"> Passwords Do Not Match </font>';		
}
}
?>
<html>
<head>
<title> Game Registration </title>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
<center>
<table width="300px">
	<form action="<?php echo $PHP_SELF; ?>" method="POST">
	<tr>
	<td>
	Username:
	</td>
	<td>
	<input type="text" name="username">
	</td>
	</tr>
	<tr>
	<td>
	Email
	</td>
	<td>
	<input type="text" name="email">
	</td>
	</tr>
	<tr>
	<td>
	Password:
	</td>
	<td>
	<input type="password" name="password">
	</td>
	</tr>
	<tr>
	<td>
	Confirm Password:
	</td>
	<td>
	<input type="password" name="confirmpassword">
	</td>
	</tr>
	<tr>
	<td>
	</td>
	<td>
	<input type="submit" name="submit" value="Register">
	</td>
	</tr>
	</form>	
</table>
<p><?php echo $error; ?></p>
</center>
</body>
</html>

 

Which is outputting:

User Added Succesfully

 

Even when the user already exists in the Database. It just keeps adding the same username over and over. Help would be great.

Is this what you need:

 

CREATE TABLE `accounts` (
  `playerid` int(11) NOT NULL AUTO_INCREMENT,
  `username` text NOT NULL,
  `password` text NOT NULL,
  `last_login` varchar(255) NOT NULL,
  PRIMARY KEY (`playerid`)
) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=latin1

Is this what you need:

 

CREATE TABLE `accounts` (
  `playerid` int(11) NOT NULL AUTO_INCREMENT,
  `username` text NOT NULL,
  `password` text NOT NULL,
  `last_login` varchar(255) NOT NULL,
  PRIMARY KEY (`playerid`)
) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=latin1

 

Yes, there's no unique keyword for your username. Try this:

 

CREATE TABLE `accounts` (

  `playerid` int(11) NOT NULL AUTO_INCREMENT,

  `username` text NOT NULL,

  `password` text NOT NULL,

  `last_login` varchar(255) NOT NULL,

  PRIMARY KEY (`playerid`),

  UNIQUE KEY playerid (playerid,username)

) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=latin1

 

 

 

 

 

Well the purpose is that you are making the playerid and username unique, in order to keep table integrity.

 

You don't want PHP to be the security blanket for checking existing usernames, if somehow someone creates

an existing username and passes the PHP test, you want the DB to stop it at its tracks.

 

Also, why are you using "Text" for username? Use varchar or char....

 

 

`username` varchar(32) NOT NULL

 

You don't want your username to be as long as whatever anyone wants. Make it either 32 or 50 characters.

 

 

 

Because you're not assigning $username to anything.

 

 

 

$checkuser = mysql_query("SELECT * FROM accounts WHERE username = '$username'");

 

 

So make it:

 

$checkuser = mysql_query("SELECT * FROM accounts WHERE username = '".$_POST[username]."' ");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.