Jump to content


Photo

Foreach Loop problem...


  • Please log in to reply
4 replies to this topic

#1 guymrodgers

guymrodgers
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 August 2006 - 06:52 PM

I'm sure that my problem is caused by a really stupid typo, but I can't find it.  I'm trying to grab a column from a database, and then go through it as an array in order to find a username.  However, my foreach loop only checks the first username in the column, and then stops.  I searched the web for a similar problem, and then I scoured the forums, and it appears that no one else has this problem, which is why I'm betting that it's a typo, but still, it's stumping me...

PLEASE HELP!!!

<?php
	$name = $_POST["compid"];
	$pass = $_POST["pass"];
	
	$con = mysql_connect("localhost","root","quantico");
	if (!$con)
             {
	    die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("argo", $con);

	$results = mysql_fetch_array(mysql_query("SELECT user FROM customers",$con));
	$correctUser = 0;
	
	foreach ($results as $value)
	{
		if ($name == $value)
		{
			$correctUser = 1;
		}
	}
	
	if ($correctUser == 0)
	{
		$theEnd = <<<END
			<script language='Javascript' type='text/javascript'>
			alert("We do not have that username on file.");
			</script>
END;
		die($theEnd);
	}

There's more code, but I didn't think it was part of the problem.  If this part of the code isn't the problem, I'll post the rest.

Thanks in advance,
Guy

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 02 August 2006 - 06:59 PM

Try this:

<?php
	$name = $_POST["compid"];
	$pass = $_POST["pass"];
	
	$con = mysql_connect("localhost","root","quantico");
	if (!$con)
             {
	    die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("argo", $con);
             $query=mysql_query("SELECT user FROM customers",$con)
	$correctUser = 0;
	
	while ($user = mysql_fetch_array($query))
	{
		if ($name == $user)
		{
			$correctUser = 1;
		}
	}
	
	if ($correctUser == 0)
	{
		$theEnd = <<<END
			<script language='Javascript' type='text/javascript'>
			alert("We do not have that username on file.");
			</script>
END;
		die($theEnd);
	}


But why dont u simply use:

<?php
	$name = $_POST["compid"];
	$pass = $_POST["pass"];
	
	$con = mysql_connect("localhost","root","quantico");
	if (!$con)
             {
	    die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("argo", $con);
             $query=mysql_query("SELECT user FROM customers WHERE user = '$name'",$con)
	$correctUser = 0;
	
	$user = mysql_fetch_array($query);
	{
		if ($user !== '')
		{
			$correctUser = 1;
		}
	}
	
	if ($correctUser == 0)
	{
		$theEnd = <<<END
			<script language='Javascript' type='text/javascript'>
			alert("We do not have that username on file.");
			</script>
END;
		die($theEnd);
	}


Then you arn't getting the whole database to the page and sorting thru for the correct username..

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 guymrodgers

guymrodgers
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 08 August 2006 - 09:03 PM

Hey thanks so much Shocker!  Your code really helped.  However, now I am still having problems with the password access...  Here's my newly updated code that I tried:

        mysql_select_db("argo", $con);
	
        $query=mysql_query("SELECT user FROM customers WHERE user = '$name'",$con);
	$correctUser = 0;
	
	$user = mysql_fetch_array($query);
	if ($user !== '')
	{
		$correctUser = 1;
	}

	if ($correctUser == 0)
	{
		$theEnd = <<<END
			<script language='Javascript' type='text/javascript'>
			alert("We do not have that username on file.");
			</script>
END;
		die($theEnd);
	}

	if($pass != mysql_query("SELECT password FROM 'customers' WHERE user=$name"))
	{
		$theEnd = <<<END
			<script language='Javascript' type='text/javascript'>
			alert("The password you entered does not match the username you provided.");
			</script>
END;
		die($theEnd);
	}


But it didn't work, so I tried a longer route:


    $resultant = mysql_query("SELECT password FROM customers WHERE user=$name",$con);
    $correctPass = 0;
    
    foreach(mysql_fetch_array($resultant) as $val)
    {
    	if ($pass == $val)
    	{
    		$correctPass = 1;
    	}
    }
    
	if ($correctPass == 0)
	{
		$theEnd = <<<END
			<script language='Javascript' type='text/javascript'>
			alert("The password you entered does not match the username you provided.");
			</script>
END;
		die($theEnd);
	}
		
	if (mysql_query("SELECT id FROM customers WHERE password=$pass") != mysql_query("SELECT id FROM customers WHERE user=$name"))
	{
   		$theEnd = <<<END
			<script language='Javascript' type='text/javascript'>
			alert("The password you entered does not match the username you provided.");
			</script>
END;
		die($theEnd);
	}

But it will only accept the first password in the list, no matter what the username is.

Help!!!

--Guy

#4 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 08 August 2006 - 09:07 PM

$resultant = mysql_query("SELECT `password` FROM `customers` WHERE `user`='$name'",$con);

I don't know if its affecting it or not, but I always use ` and ' around my mysql queries...
Why doesn't anyone ever say hi, hey, or whad up world?

#5 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 08 August 2006 - 09:15 PM

and really you could just use

$q = mysql_query("SELECT * FROM `customers` WHERE `user`='$name' AND `password`='$pass'",$con);
$num = @mysql_num_rows($q);
if($num == "1") {
//stuff to do if the password is correct
}
else {
//stuff to do if the password is incorrect
}

also
if (mysql_query("SELECT id FROM customers WHERE password=$pass") != mysql_query("SELECT id FROM customers WHERE user=$name"))
I dont think you can compare straight queries... I think you would have to turn them into results... Also 2 users could have the same password so lets say $name = zabc and the other person who has their password is named aabc then aabc's id would be returned first by the first one, but zabc's id would be selected from the second...

Why doesn't anyone ever say hi, hey, or whad up world?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users