Wolphie Posted November 17, 2007 Share Posted November 17, 2007 Ok so with the following code i'm trying to check the database for already used usernames and e-mail addresses. I'm not entirely sure if this is working or not, but all i'm getting back is a blank page. function checkDetails() { $username = mysql_escape_string($_POST['username']); // Retrieve username from registration form $email = mysql_escape_string($_POST['email']); // Retrieve e-mail from registration form $sql = mysql_query(sprintf("SELECT `username`, `email` FROM `users`")) or die('Error: ' . mysql_error()); while($obj = mysql_fetch_object($sql)) { if(($obj->username) == $username || ($obj->email) == $email) { ?> <table width="500" cellpadding="0" cellspacing="0" class="mainBody" align="center"> <tr> <td align="center" class="headTitle"> <u>Registration Failed!</u> </td> </tr> <tr> <td align="center"> <p> The username or e-mail address you have chosen is currently in use. Please use a different username or e-mail address. </p> </td> </tr> </table> <? } else { checkReg(); } } } checkDetails(); Quote Link to comment Share on other sites More sharing options...
revraz Posted November 17, 2007 Share Posted November 17, 2007 Do a Google search for checkUnique You'll find some good source code. Quote Link to comment Share on other sites More sharing options...
marcus Posted November 17, 2007 Share Posted November 17, 2007 $sql = "SELECT * FROM `users` WHERE `username`='$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0){ echo "Username taken!\n"; }else { // do email here } Quote Link to comment Share on other sites More sharing options...
Orio Posted November 17, 2007 Share Posted November 17, 2007 Instead of fetching all of your data, just check how many records contain the given user/email: <?php function checkDetails() { $username = mysql_escape_string($_POST['username']); // Retrieve username from registration form $email = mysql_escape_string($_POST['email']); // Retrieve e-mail from registration form $sql = mysql_query(sprintf("SELECT * FROM `users` WHERE username='$username' OR email='$email'")) or die('Error: ' . mysql_error()); if(mysql_num_rows($sql) > 0) echo "Choose something different!"; } ?> Orio. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted November 17, 2007 Author Share Posted November 17, 2007 Thanks for your help guys. Much appreciated! Quote Link to comment Share on other sites More sharing options...
kellz Posted November 17, 2007 Share Posted November 17, 2007 $check = (mysql_fetch_array(mysql_query( "SELECT `email`, `loginName` FROM `users` WHERE loginName='$username' AND email='$email'")) ); if ($NewUser == $check[1] || $NewEmail == $check[0]) { echo 'that username and/or email address is already in use.'; exit; } oowwps i posted this seconds after you made it solved^^ 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.