Jump to content

[SOLVED] username check


sstangle73

Recommended Posts

the function is supposed to catch when the username is is use but when tested with a username that is in use it returns good

 

<?php
function validateusername($val){

global $continueSubmit ;

function usernameTaken($val){
$sql = "SELECT * FROM `user` WHERE `username` = '" . $val . "'	LIMIT 1";
$result = mysql_query($sql);
return (mysql_numrows($result) > 0);
}

$userlen = strlen($val);
//check for blank username
if($val == ''){
	$continueSubmit = false;
	echo "<img src=\"/images/bad.gif\" alt=\"Bad\"> Field Required";
	} else
if(usernameTaken($val)){
	$continueSubmit = false;
	echo "<img src=\"/images/bad.gif\" alt=\"Bad\"> Username in use";
} else
if($userlen > 50){
	$val = false;
	echo "<img src=\"/images/bad.gif\" alt=\"Bad\"> Username Too Long. Please enter a username between 5 and 50 characeters.";
	} else
if($userlen < 5){
	$val = false;
	echo "<img src=\"/images/bad.gif\" alt=\"Bad\"> Username Too Short. Please enter a username between 5 and 50 characeters.";
	}
else {
//everything ok with username
echo "<img src=\"/images/good.gif\" alt=\"Good\">";
}
}
?>

 

. . .  edit .  . . talking to someone heres the problem discribed a little better

 

Max says:

what is the outputted error?

AG | Blkout says:

no error

AG | Blkout says:

it comes back as good.

AG | Blkout says:

the bottom of the else list

AG | Blkout says:

but it should fail on the if(usernameTaken) line

Link to comment
https://forums.phpfreaks.com/topic/115868-solved-username-check/
Share on other sites

You have bad logic going on there.  And that code is so messy.  Honestly.  What's the point of declaring the function inside another function, and what's the point of the whole validateusername() function anyway if you're just going to use it once?

 

Also, you misspelled mysql_num_rows().

if (!usernameTaken($val)) {

...

...

}

 

Try that. =P  You have bad logic going on there.  And that code is so messy.  Honestly.  What's the point of declaring the function inside another function, and what's the point of the whole validateusername() function anyway if you're just going to use it once?

neither of max's helped and this made it always fail

Try this:

 

<?php
function validateusername($val){

global $continueSubmit ;

$result  = mysql_query("SELECT * FROM `user` WHERE `username` = '$val'	LIMIT 1");
$numrows = mysql_num_rows($result);

$userlen = strlen($val);
//check for blank username
if($val == ''){
	$continueSubmit = false;
	echo "<img src=\"/images/bad.gif\" alt=\"Bad\"> Field Required";
	} else
if($numrows > 0){
	$continueSubmit = false;
	echo "<img src=\"/images/bad.gif\" alt=\"Bad\"> Username in use";
} else
if($userlen > 50){
	$val = false;
	echo "<img src=\"/images/bad.gif\" alt=\"Bad\"> Username Too Long. Please enter a username between 5 and 50 characeters.";
	} else
if($userlen < 5){
	$val = false;
	echo "<img src=\"/images/bad.gif\" alt=\"Bad\"> Username Too Short. Please enter a username between 5 and 50 characeters.";
	}
else {
//everything ok with username
echo "<img src=\"/images/good.gif\" alt=\"Good\">";
}
}
?>

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.