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

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

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

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.

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

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

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.

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>

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.