ELSE IN " DO WHILE LOOP "

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

ELSE IN " DO WHILE LOOP "

Post by Skywalker »

What am I doing worng. I want to show a tekst when the account isn't in the database.

My script:

$Password = $_POST['Password'];
$Username = $_POST['Username'];

do {
If($Password == $row_Recordset1['Password'] && $Username == $row_Recordset1['Username']) {

echo ("Login gegevens zijn corect <META HTTP-EQUIV=\"refresh\" content=\"2;URL=admin-html.php\">\n"); }

else { echo ("Login is incorect <META HTTP-EQUIV=\"refresh" content=\"2;URL=login.php\">\n"); }


} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));


with out the bolt php line it works fine but with it, i doesn't do enything :/

[/b]
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Change the

Code: Select all

HTTP-EQUIV="refresh"
to

Code: Select all

HTTP-EQUIV="refresh"
Image Image
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

Post by Skywalker »

I correct it, but when I am typing pass en username correct in, this appears


Login gegevens zijn niet correct Login gegevens zijn niet correct

and is sending me back to login.php insted to the admin-html.php




THIS TEXT: Login gegevens zijn niet correct Login gegevens zijn niet correct

must be seperated. It shows the tekst twice, because ther are two records in the table.






$Password = $_POST['Password'];
$Username = $_POST['Username'];
$Leeg1= 0;

do {
If($Password == $row_Recordset1['Password'] && $Username == $row_Recordset1['Username']) {

echo ("Login gegevens zijn corect <META HTTP-EQUIV=\"refresh\" content=\"2;URL=admin-html.php\"><br>\n"); }

else {
echo("Login gegevens zijn niet correct <META HTTP-EQUIV=\"refresh\" content=\"2;URL=login.php\">\n"); }

} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

In your SQL statement put LIMIT 1 at the end and add your test for username and password, eg:

Code: Select all

$sql = "SELECT username, password FROM users WHERE username='".$_POST&#1111;'username']."' AND password='".$_POST&#1111;'password']."' LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());
then only one record will be returned and you won't need the do...while loop but can test if the username and password are valid by doing:

Code: Select all

if (mysql_num_rows($result) == 1) {
    echo "&lt;p&gt;Login gegevens zijn corect&lt;/p&gt; &lt;META HTTP-EQUIV="refresh" content="2;URL=admin-html.php"&gt;\n";
} else {
    echo "&lt;p&gt;Login is incorect&lt;/p&gt; &lt;META HTTP-EQUIV="refresh" content="2;URL=login.php"&gt;\n";
}
Also, have you encrypted the password in the database? If so you'll need to encrypt the posted password in order to compare it with what's in the database.

Mac
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

Post by Skywalker »

It still doesn't work correct. It still doesn't read the second record in the table.

And it shows both texts when login is faild and correct. It shows everytime both texts.

This is my full script:

<?php require_once('../Connections/Verbinding.php'); ?>
<?php
mysql_select_db($database_Verbinding, $Verbinding);
$query_Recordset1 = "SELECT * FROM user";
$Recordset1 = mysql_query($query_Recordset1, $Verbinding) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);



$sql = "SELECT username, password FROM user WHERE username='".$_POST['username']."' AND password='".$_POST['password']."' LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result) == 1) {
echo "<p>Login gegevens zijn corect</p> <META HTTP-EQUIV=\"refresh\" content=\"2;URL=admin-html.php\">\n";
} else {
echo "<p>Login is incorect</p> <META HTTP-EQUIV=\"refresh\" content=\"2;URL=login.php\">\n";
}




/*
$Password = $_POST['Password'];
$Username = $_POST['Username'];
$Leeg1= 0;

do {
If($Password == $row_Recordset1['Password'] && $Username == $row_Recordset1['Username']) {

echo ("Login gegevens zijn corect <META HTTP-EQUIV=\"refresh\" content=\"2;URL=admin-html.php\"><br>\n"); }

else {
echo("Login gegevens zijn niet correct <META HTTP-EQUIV=\"refresh\" content=\"2;URL=login.php\">\n"); }

} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
*/
?>


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>



</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you encrypted the password in the database? If so you'll need to encrypt the posted password in order to compare it with what's in the database.

Mac
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

Post by Skywalker »

No, because it beeing used only on the local network. It works fine, but it reads only the first record and not all of them, and both texts are been showed everytime, that is the only thing.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

What happens if you do:-

Code: Select all

&amp;lt;?php
$sql = "SELECT username, password FROM user WHERE username='".$_POST&#1111;'username']."' AND password='".$_POST&#1111;'password']."' LIMIT 1"; 
$result = mysql_query($sql) or die(mysql_error()); 

echo mysql_num_rows($result);
?&amp;gt;
Post Reply