Russia Posted March 22, 2011 Share Posted March 22, 2011 Hey guys, I have this little script, but it doesn't seem to work properly. <?php mysql_connect("localhost","root",""); mysql_select_db("chat"); $user = strip_tags(trim($_REQUEST['username'])); if(strlen($user) <= 0) { echo json_encode(array('code' => -1,'result' => 'Invalid username, please try again.')); die; } // Query database to check if the username is available $query = "Select * from users where username = '$user' "; $result = mysql_query($query); $avaliable = mysql_num_rows($result); // Execute the above query using your own script and if it return you the // result (row) we should return negative, else a success message. //$available = true; if($available) { echo json_encode(array('code' => 1,'result' => "Success,username $user is still available")); die; } else { echo json_encode(array('code' => 0,'result' => "Sorry but username $user is already taken.")); die; } die; ?> It keeps returning with 'Sorry but username iunsdbsyusaddssd763b373b is already taken'. Even when I type in some random gibberish like 'iunsdbsyusaddssd763b373b' Can anyone show me why it is doing that? Thanks alot friends. Quote Link to comment https://forums.phpfreaks.com/topic/231442-username-avaliability-checker-keeps-returning-taken/ Share on other sites More sharing options...
trq Posted March 22, 2011 Share Posted March 22, 2011 Are you sure your query is even working? You haven't bothered to check. Quote Link to comment https://forums.phpfreaks.com/topic/231442-username-avaliability-checker-keeps-returning-taken/#findComment-1191078 Share on other sites More sharing options...
Russia Posted March 22, 2011 Author Share Posted March 22, 2011 I have checked it over and I think its right. So if possible, what is correct way of making it check and give an available or not available message? Quote Link to comment https://forums.phpfreaks.com/topic/231442-username-avaliability-checker-keeps-returning-taken/#findComment-1191079 Share on other sites More sharing options...
trq Posted March 22, 2011 Share Posted March 22, 2011 An example of the logic involved in executing a select query. $sql = "SELECT fld FROM tbl"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // it is safe to use $result. } else { // no records returned } } else { // query failed, handle error. } Quote Link to comment https://forums.phpfreaks.com/topic/231442-username-avaliability-checker-keeps-returning-taken/#findComment-1191080 Share on other sites More sharing options...
Russia Posted March 22, 2011 Author Share Posted March 22, 2011 Okay, I changed it to this: <?php mysql_connect("localhost","root",""); mysql_select_db("chat"); $user = strip_tags(trim($_REQUEST['username'])); if(strlen($user) <= 0) { echo json_encode(array('code' => -1,'result' => 'Invalid username, please try again.')); die; } // Query database to check if the username is available $sql = "Select * from users where username = '$user' "; //$result = mysql_query($query); //$avaliable = mysql_num_rows($result); // Execute the above query using your own script and if it return you the // result (row) we should return negative, else a success message. //$available = true; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { echo json_encode(array('code' => 1,'result' => "Success,username $user is still available")); die; } else { echo json_encode(array('code' => 0,'result' => "Sorry but username $user is already taken.")); die; } } ?> It still returns as {"code":1,"result":"Success,username admin is still available"} What seems to be the problem? Quote Link to comment https://forums.phpfreaks.com/topic/231442-username-avaliability-checker-keeps-returning-taken/#findComment-1191082 Share on other sites More sharing options...
Russia Posted March 22, 2011 Author Share Posted March 22, 2011 Okay thanks, I managed to fix it by just switching the Avaliable and not avaliable parts. Quote Link to comment https://forums.phpfreaks.com/topic/231442-username-avaliability-checker-keeps-returning-taken/#findComment-1191083 Share on other sites More sharing options...
trq Posted March 22, 2011 Share Posted March 22, 2011 Your logic is backward. Your saying if the record exists that it doesn't and vice versa. Quote Link to comment https://forums.phpfreaks.com/topic/231442-username-avaliability-checker-keeps-returning-taken/#findComment-1191085 Share on other sites More sharing options...
Russia Posted March 23, 2011 Author Share Posted March 23, 2011 Thanks, also, one more thing, how would I do it so it enables the submit button once the message is Avaliable username. I have this code: <script type="text/javascript"> //function to create ajax object function pullAjax(){ var a; try{ a=new XMLHttpRequest() } catch(b) { try { a=new ActiveXObject("Msxml2.XMLHTTP") }catch(b) { try { a=new ActiveXObject("Microsoft.XMLHTTP") } catch(b) { alert("Your browser broke!");return false } } } return a; } function validate() { site_root = ''; var x = document.getElementById('uname'); var msg = document.getElementById('msg'); var submituser = document.getElementById('submituser'); user = x.value; code = ''; message = ''; obj=pullAjax(); obj.onreadystatechange=function() { if(obj.readyState==4) { eval("result = "+obj.responseText); code = result['code']; message = result['result']; if(code <=0) { x.style.border = "1px solid red"; msg.style.color = "red"; } else { x.style.border = "1px solid #000"; msg.style.color = "green"; usersubmit.style.display = 'block'; } msg.innerHTML = message; } } obj.open("GET",site_root+"js/user_availability.php?username="+user,true); obj.send(null); } </script> and the form is here: echo "<form action='' method='POST'>"; echo ' User Name : <input type="text" onblur="validate();" id="uname" name="uname" value="" /> <div id="msg"></div>'; echo "next info"; echo "<input name='step3' value='Next' id='submituser' style='display:none' type='submit' />"; echo "</form>"; I set it as display none as a default and then when the box is green for the box to show as a block. Its not showing up tho after the box becomes green Quote Link to comment https://forums.phpfreaks.com/topic/231442-username-avaliability-checker-keeps-returning-taken/#findComment-1191093 Share on other sites More sharing options...
trq Posted March 23, 2011 Share Posted March 23, 2011 This is a completely different question and belongs in the Ajax board. I suggest you open a new thread there. While your in the Ajax board you should read the very first sticky, no one writes there own Ajax methods these days. Quote Link to comment https://forums.phpfreaks.com/topic/231442-username-avaliability-checker-keeps-returning-taken/#findComment-1191102 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.