Jump to content

Foreach Loop problem...


guymrodgers

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
Link to comment
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
Link to comment
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
Link to comment
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...
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.