Jump to content

Archived

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

guymrodgers

Foreach Loop problem...

Recommended Posts

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

[code]
<?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);
}
[/code]

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

Share this post


Link to post
Share on other sites
Try this:

[code]<?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);
}
[/code]


But why dont u simply use:

[code]<?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);
}
[/code]


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

Regards
Liam

Share this post


Link to post
Share on other sites
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:

[code]
        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);
}

[/code]

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

[code]

    $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);
}
[/code]

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

Help!!!

--Guy

Share this post


Link to post
Share on other sites
$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...

Share this post


Link to post
Share on other sites
and really you could just use

[code=php:0]
$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
}
[/code]

also
[code=php:0]if (mysql_query("SELECT id FROM customers WHERE password=$pass") != mysql_query("SELECT id FROM customers WHERE user=$name"))[/code]
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...

Share this post


Link to post
Share on other sites

×

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.