Jump to content

how can I write out a loop inside this array?


jarv

Recommended Posts

//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');

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.