Jump to content

validating that a username doesnt already exist


Lodius2000

Recommended Posts

I am trying to make an account creation page and I want to make sure that the proposed username doesnt already exist, but i cant figure how

 

here is what I have

 

//this code uses PEAR db
$user_check = $db->query('SELECT username FROM users WHERE username = (?)',
	array($_POST['username']));
if ($user_check > 0){
	$errors[] = "Your username is already in use, please choose another";
}

 

will post full code if requested,

 

Thanks in advance

Link to comment
Share on other sites


$result = mysql_query("SELECT username FROM users WHERE username = '$_POST[username]'");

if ( mysql_num_rows($result) > 0 )
{
echo 'Username exists';
}
else
{
// stuff
}

 

(Used $_POST['username'] as an example)

Link to comment
Share on other sites

this is the current code

 

adapted from pear db as best I could

 

$q = $db->query("SELECT username FROM users WHERE username = '$_POST[username]'");
if ($q->numrows() > 0 ){
	$errors[] = "Your username is already in use, please choose another";
}

 

but if i follow in the examples already listed I need something inside the numrows function but there is nothing to put there because this prints

 

DB Error: no such field

 

upon submission of the desired username

Link to comment
Share on other sites

I forget what they call it, but I never have understood PHP code written with the -> things.

 

Nonetheless, this may be of some use. It is some code I'm working on for a registration script. It would probably be best to put each check in to a "case", but I'm not the one to ask on how to do that. :)

 

	// Sanitize the POST values
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$cpass = $_POST['cpass'];

// Input Validations
$check = mysql_num_rows(mysql_query("SELECT * FROM members WHERE uname='$uname'"));

if($fname == '' | $lname == '' | $uname == '' | $pass == '' | $cpass == '' | strcmp($pass, $cpass) != 0 | $check > 0) {

	echo "<p>The following fields are missing. Please go back and correct these fields.</p>\n\n<ul>\n";

	if($fname == '') {
		echo "<li>First name missing</li>\n";
	}
	if($lname == '') {
		echo "<li>Last name missing</li>\n";
	}
	if($uname == '') {
		echo "<li>Username missing</li>\n";
	}
	if($pass == '') {
		echo "<li>Password missing</li>\n";
	}
	if($cpass == '') {
		echo "<li>Confirm password missing</li>\n";
	}
	if(strcmp($pass, $cpass) != 0) {
		echo "<li>Passwords do not match</li>\n";
	}
	if($check > 0) {
		echo "<li>Username already in use</li>\n";
	}

	echo "</ul>\n\n";

}

Link to comment
Share on other sites

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.