Lodius2000 Posted May 23, 2008 Share Posted May 23, 2008 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 https://forums.phpfreaks.com/topic/106878-validating-that-a-username-doesnt-already-exist/ Share on other sites More sharing options...
LooieENG Posted May 23, 2008 Share Posted May 23, 2008 $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 https://forums.phpfreaks.com/topic/106878-validating-that-a-username-doesnt-already-exist/#findComment-547863 Share on other sites More sharing options...
Lodius2000 Posted May 23, 2008 Author Share Posted May 23, 2008 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? Link to comment https://forums.phpfreaks.com/topic/106878-validating-that-a-username-doesnt-already-exist/#findComment-547871 Share on other sites More sharing options...
DarkerAngel Posted May 23, 2008 Share Posted May 23, 2008 Dose you have the DB structure in place? also have it output the exact query for debugging Link to comment https://forums.phpfreaks.com/topic/106878-validating-that-a-username-doesnt-already-exist/#findComment-547874 Share on other sites More sharing options...
mjcoco Posted May 23, 2008 Share Posted May 23, 2008 Maybe alittle more code? Make sure your table name is correct, as in case. Link to comment https://forums.phpfreaks.com/topic/106878-validating-that-a-username-doesnt-already-exist/#findComment-547878 Share on other sites More sharing options...
Lodius2000 Posted May 23, 2008 Author Share Posted May 23, 2008 table is correct, with out this validation line the script works, but it will enter the same username twice, which is what this should prevent Link to comment https://forums.phpfreaks.com/topic/106878-validating-that-a-username-doesnt-already-exist/#findComment-547885 Share on other sites More sharing options...
Lodius2000 Posted May 23, 2008 Author Share Posted May 23, 2008 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 https://forums.phpfreaks.com/topic/106878-validating-that-a-username-doesnt-already-exist/#findComment-547888 Share on other sites More sharing options...
gamefreak13 Posted May 23, 2008 Share Posted May 23, 2008 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 https://forums.phpfreaks.com/topic/106878-validating-that-a-username-doesnt-already-exist/#findComment-547891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.