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

when i change my code I get an error after i submit a proposed username

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /createuser1.php on line 75

DB Error: no such field

 

any ideas?

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

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";

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.