Error: wrong username or password x10

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

Error: wrong username or password x10

Post 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!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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'");
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

Post 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?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

while creates a loop, you dont need a loop to compare n contrast username with passwords.

use if statements :wink:
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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";
}
?>
Last edited by tim on Sun Apr 18, 2004 3:53 pm, edited 1 time in total.
yosuke_
Forum Commoner
Posts: 64
Joined: Tue Apr 13, 2004 12:29 pm

Post by yosuke_ »

THANK YOU VERY MUCH!!! your my hero!! :wink:
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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:
Post Reply