sstangle73 Posted July 21, 2008 Share Posted July 21, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 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(). Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 21, 2008 Share Posted July 21, 2008 Change usernameTaken to: function usernameTaken($val){ $result = mysql_query("SELECT * FROM `user` WHERE `username` = '$val' LIMIT 1"); return (mysql_numrows($result) > 0); } Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 21, 2008 Share Posted July 21, 2008 I would advise not using a function for something as simple as checking if a username exists . You might want to rethink your code logic. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 21, 2008 Share Posted July 21, 2008 you spelled mysql_num_rows as mysql_numrows Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 21, 2008 Share Posted July 21, 2008 Change usernameTaken to: function usernameTaken($val){ $result = mysql_query("SELECT * FROM `user` WHERE `username` = '$val' LIMIT 1"); return (mysql_numrows($result) > 0); } That makes no difference.... Quote Link to comment Share on other sites More sharing options...
sstangle73 Posted July 21, 2008 Author Share Posted July 21, 2008 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 Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 21, 2008 Share Posted July 21, 2008 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\">"; } } ?> Quote Link to comment Share on other sites More sharing options...
sstangle73 Posted July 21, 2008 Author Share Posted July 21, 2008 got it thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.