How to avoid same userName in database?

Started on the old forum in September 2011 · view the original in the archive · old links redirect here

#debugg #error #mysql #php #wap-chat

2 replies · 743 views · started 15 years ago · 1 min read

3 participants
s3nzo Developer · 156 posts #1

Hi guys how do i avoid same user name being used more than once in a database? like if my database already has a user name s3nzo it must reject other registration with the same name or same email here is my code

<?php
	require	'inc/head.inc';
	
		$loginName	=	mysql_real_escape_string(htmlentities(stripslashes(trim($_POST['loginName']))));
		$email		=	mysql_real_escape_string(htmlentities(stripslashes(trim($_POST['email']))));
		$pass		=	mysql_real_escape_string(trim(htmlentities(stripslashes(trim(md5($_POST['pass']))))));
		$pas2		=	mysql_real_escape_string(trim(htmlentities(stripslashes(trim(md5($_POST['pas2']))))));
		$_SESSION['loginName']	=	$loginName;
		$_SESSION['email']		=	$email;
		$_SESSION['pass']		=	$pass;
	$sqle	=	"SELECT * FROM users";
	$querye	=	@mysql_query($sqle, $con);

			$errors	=	array();
				$fetch	=	@mysql_fetch_array($querye);
					
						if($_SESSION['loginName'] == $fetch['loginName'])
							$errors	=	'Login name already taken';
						if($_SESSION['email']		== $fetch['email'])
							$errors	=	'Email address already in use';
					
				if(empty($loginName))
					$errors	=	'Login name can not be empty';
				if(empty($email))
					$errors	=	'Email can not be empty';
				if(empty($pass) || empty($pas2))
					$errors	=	'Both passwords are required';
				if($pass != $pas2)
					$errors	=	'Passwords must be the same';
					
				if(count($errors))
					{
						$_SESSION['errors']	=	$errors;

						header('Location:register.php');
					}
				else
					{
						$sql	=	"INSERT INTO users	SET	loginName	=	'{$_SESSION['loginName']}',
															email		=	'{$_SESSION['email']}',
															password	=	'{$_SESSION['pass']}',
															id			=	'{NULL}',
															nick		=	'{$_SESSION['loginName']}'";
						if(!$query	=	@mysql_query($sql, $con))
							$forms->showError();
						else
						{
							$sql2	=	"SELECT * FROM users WHERE loginName	=	'{$_SESSION['loginName']}'";
							if(!$query2	=	@mysql_query($sql2, $con))
								$forms->showError();
							else
								{
									if(!$rows	=	@mysql_fetch_array($query2))
										$forms->showError();
										else
											{
												
												$_SESSION['id']			=	$rows['id'];
												$_SESSION['nick']		        =	$rows['nick'];
												$_SESSION['fname']		=	$rows['fname'];
												$_SESSION['sname']		=	$rows['sname'];
												$_SESSION['perms']		=	$rows['permissions'];
												header('Location:home.php');
											}
								}
						}
					}
				
	require	'inc/foot.inc';
?>

it rejects the registration of the same user name only once if you try again it inserts the details into database #########################################################################

NEVER mind guys problem solved

something else Fellow · 2,296 posts #2

eaiest way to prevent this from happening is to alter your sql:

ALTER TABLE users
ADD UNIQUE (loginName,email);
CreativityKills Lead · 833 posts #3

Then u myt in addition to what he said set database charset to UTF8. That way, sënzø and senzo are the same n cant be registered.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close