Page 1 of 1

Error: wrong username or password x10

Posted: Sun Apr 18, 2004 2:29 pm
by yosuke_
Hi!
I have login script here, but the problem is when I enter invalid username or password I get this:
Invalid User Name or Password
Invalid User Name or Password
Invalid User Name or Password
Invalid User Name or Password
Invalid User Name or Password
Invalid User Name or Password
Invalid User Name or Password
Invalid User Name or Password
Invalid User Name or Password
Invalid User Name or Password

My code is this:

Code: Select all

<?
$username = $_POST["txtusername"];
$password = $_POST["txtpassword"];
	$db = mysql_connect('localhost','root','mypass') ; 
	mysql_select_db("database") 
	$result = mysql_query("SELECT * FROM users"); 
	while($results = mysql_fetch_array($result)){
	if ($results['username'] == $username && base64_decode($results['password']) == $password) {
echo "You are loged in successfully";
}
else
{
echo "Invalid User Name or Password";
}

}
?>
How can I make Invalid User Name or Password display only once?? I try Break; but then It stops searching after the first username! Can you please help me? thank you!

Posted: Sun Apr 18, 2004 2:44 pm
by tim
well

$result = mysql_query("SELECT * FROM users");

you need to specify a row

it gives 10 Invalid User Name or Password caus I bet theres 10 rows of data.

try:
$result = mysql_query("SELECT * FROM users WHERE username_field_name='$username'");

Posted: Sun Apr 18, 2004 3:03 pm
by yosuke_
well, I have something wrong in my code! it dosnt work!!

Code: Select all

$username = $_POST["txtusername"];
$password = $_POST["txtpassword"];
	$db = mysql_connect('localhost','root','mypass') or die(mysql_error()); 
	mysql_select_db("database") or die(mysql_error()); 
	$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'"); 
	while($results = mysql_fetch_array($result)){
	echo "Loged In Successfully<br>";
		}
what is wrong?

Posted: Sun Apr 18, 2004 3:33 pm
by tim
while creates a loop, you dont need a loop to compare n contrast username with passwords.

use if statements :wink:

Posted: Sun Apr 18, 2004 3:52 pm
by tim
if u need further help:

Code: Select all

<?php
$sql = "SELECT * FROM users WHERE username='$username'";
$result= mysql_query($sql);
$row= mysql_fetch_array($result); // you'll need to make the data in the abel into a array

$name = $row["username"];
$pass = $row["password"];

if ($username == $name && password==$pass) {
echo "you are logged in";
} else {
echo "incorrect name and/or password";
}
?>

Posted: Sun Apr 18, 2004 3:53 pm
by yosuke_
THANK YOU VERY MUCH!!! your my hero!! :wink:

Posted: Sun Apr 18, 2004 3:55 pm
by tim
yosuke_ wrote:I allready have loop!! but can you please give me some code example??Thank you!
No <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>

I meant to say you DONT need the loop, its useless in this case.. You would use a loop to DISPLAY all the table contents.

Read my posts before u explode. and if u did a search here you would find this topic has been covered extensivly over n over again.

:evil: