jarv Posted December 7, 2010 Share Posted December 7, 2010 //this varible contains the array of existing users $existing_users=array('roshan','mike','jason'); //value got from the get metho $user_name=$_POST['user_name']; //checking weather user exists or not in $existing_users array if (in_array($user_name, $existing_users)) { //user name is not available echo "no"; } else { //username available i.e. user name doesn't exists in array echo "yes"; } the array near the top has a list of users, I would like to loop through my users in my database I know how to write a loop but not into here $existing_users=array('roshan','mike','jason'); Quote Link to comment Share on other sites More sharing options...
Rifts Posted December 7, 2010 Share Posted December 7, 2010 you want to do something like this then connect to db //get username $user = $_POST['user_name']; //looking to db to see if user exists $sql = mysql_query("select * from table where name='$user_name'"); $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ echo "yes"; else { echo "no"; } obviously this is a really rough draft but it should help Quote Link to comment Share on other sites More sharing options...
jarv Posted December 8, 2010 Author Share Posted December 8, 2010 here is my code: <? session_start(); include_once("config.php"); $ebits = ini_get('error_reporting'); error_reporting($ebits ^ E_NOTICE);; checkLoggedIn("no"); //get username $user = $_POST['user_name']; //looking to db to see if user exists $sql = mysql_query("select * from members_copy where RSUSER='".$user."'"); $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ echo "yes"; else { echo "no"; } ?> it doesn't work, It always shows YES?! Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 8, 2010 Share Posted December 8, 2010 First, I can't see anyway that would not work unless you are entering a username which already exists. The sql query is fine and is certainly not going to return a row where username is not what is contained within $user variable. First check is to echo out $user, then if it IS echoing 'yes', echo out that row which it has found. Compare $user to the RSUSER column. To echo out the row use this: if($count==1){ echo "<pre>"; print_r($result); echo "</pre>"; else { echo "no"; } Second, do NOT use $user variable without first escaping it first. $user = $_POST['user_name']; ..high security issue. Should be: $user = mysql_real_escape_string($_POST['user_name']); ..you just made your code 10 times stronger. Come back once you have printed out the row if the count is 1 as we should be able to see the problem from that. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 8, 2010 Share Posted December 8, 2010 Is there one record in the table with an empty value in the RSUSER field, by chance? Also, don't get in the habit of using the short php open <? tags. To save yourself future headaches, use the full syntax: <?php Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 8, 2010 Share Posted December 8, 2010 Is there one record in the table with an empty value in the RSUSER field, by chance? Also, don't get in the habit of using the short php open <? tags. To save yourself future headaches, use the full syntax: <?php Thanks for that insight! Didn't realize that would also return a row. You've saved me some future headaches too! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 8, 2010 Share Posted December 8, 2010 I guess I should clarify that a bit. It could return a record if the form is submitted with the username form field empty. The value should be validated, and there should be logic to make sure the form has actually been submitted before allowing the query to run. Quote Link to comment Share on other sites More sharing options...
jarv Posted December 8, 2010 Author Share Posted December 8, 2010 I am still getting YES Here is the ALL the code PHP <? session_start(); include_once("config.php"); $ebits = ini_get('error_reporting'); error_reporting($ebits ^ E_NOTICE);; checkLoggedIn("no"); //get username $user = $_POST['user_name']; //looking to db to see if user exists $sql = mysql_query("select * from members_copy where RSUSER='".$user."'"); $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ echo "<pre>"; print_r($result); echo "</pre>"; else { echo "no"; } ?> JavaScript <script language="javascript"> //<!---------------------------------+ // Developed by Roshan Bhattarai // Visit http://roshanbh.com.np for this script and more. // This notice MUST stay intact for legal use // ---------------------------------> $(document).ready(function() { $("#RSUSER").blur(function() { //remove all the class add the messagebox classes and start fading $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow"); //check the username exists or not from ajax $.post("user_availability.php",{ user_name:$(this).val() } ,function(data) { if(data=='no') //if username not avaiable { $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html('<img src="images/cross.png" />').addClass('messageboxerror').fadeTo(900,1); }); } else { $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html('<img src="images/tick.png" />').addClass('messageboxok').fadeTo(900,1); }); } }); }); }); </script> html <input name="RSUSER" type="text" class="textbox" id="RSUSER" value="" /> <span id="msgbox" style="display:none"></span> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 8, 2010 Share Posted December 8, 2010 I just spotted something. You're executing the query twice. Once with the query string, and once with the result resource from the previous execution. This needs to be changed: //looking to db to see if user exists $sql = mysql_query("select * from members_copy where RSUSER='".$user."'"); $result=mysql_query($sql); To this: //looking to db to see if user exists $sql = "select * from members_copy where RSUSER = '$user'"; $result=mysql_query($sql); Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 8, 2010 Share Posted December 8, 2010 Since it appears the form field name is RSUSER, not username, you need to change $_POST['username'] to $_POST['RSUSER']. And are you sure that is the PHP code you're currently executing? It has a parse error and shouldn't even run as it is. Quote Link to comment Share on other sites More sharing options...
jarv Posted December 8, 2010 Author Share Posted December 8, 2010 ok thanks, changed that, still just keeps on saying YES http://www.mypubspace.com/register.php already existing in the database JAX john mike Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 8, 2010 Share Posted December 8, 2010 You need to come back with the message it is printing. I had it print the row it found. We need to see that and exactly what is contained with $Uname to get a better picture of what might be wrong. Quote Link to comment Share on other sites More sharing options...
jarv Posted December 8, 2010 Author Share Posted December 8, 2010 if($count==1){ echo "<pre>"; print_r($result); echo "</pre>"; else { echo "no"; } was not working when I was using this?! it didn't echo anything out?! Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 8, 2010 Share Posted December 8, 2010 Then that code is not being executed. Both statements cover every possible scenario. It is either 1 or it isn't. There is no way to bypass that, the else statement makes sure of it. Quote Link to comment Share on other sites More sharing options...
jarv Posted December 10, 2010 Author Share Posted December 10, 2010 so how would I execute the code?! 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.