Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP MYSQL problems

Status
Not open for further replies.

zura

Technical User
Sep 20, 2011
23
GE
HI!
I have problem with this php scripts.
I write this script for check db. It works correctly but, checks only first login and password.
...thank you for help.
Code:
<html>
<head>
</head>
<body>

<form method="post">
login:<input name="login" type="text" > <br /><br />
password:<input name="password" type="password" >  <br /><br />
<input type="submit" value="ok!" name="button">
</form>

<?php
$db = mysql_connect("localhost","user","");
mysql_select_db("dbname",$db);

$button=$_POST['button'];
if($_POST['button']==true){
 $login=$_POST['login'];
 $password=$_POST['password'];
 $query = "SELECT login, password FROM this";
	$result = mysql_query($query) or die (mysql_error());
	$res = mysql_fetch_array($result);
    if ($res[0] == $login){		
		if($res[1] == $password)  {echo "sucess";} /* or header */
		else{echo "incorrect password?";}
	}
	else{echo "incorrect name";}
}
?>

</body>
</html>
 
Hi

Wrong approach. Do not query all user data just to check one.
Code:
  [navy]$query[/navy] [teal]=[/teal] [green][i]"SELECT password FROM this WHERE login='"[/i][/green] [teal].[/teal] [COLOR=darkgoldenrod]mysql_real_escape_string[/color][teal]([/teal][navy]$login[/navy][teal])[/teal] [teal].[/teal] [green][i]"'"[/i][/green][teal];[/teal]
  [navy]$result[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_query[/color][teal]([/teal][navy]$query[/navy][teal])[/teal] [b]or[/b] [b]die[/b] [teal]([/teal][COLOR=darkgoldenrod]mysql_error[/color][teal]());[/teal]
  [navy]$res[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_fetch_array[/color][teal]([/teal][navy]$result[/navy][teal]);[/teal]
  [b]if[/b] [teal]([/teal][navy]$res[/navy] [teal]!==[/teal] [b]false[/b][teal])[/teal] [teal]{[/teal]
    [b]if[/b] [teal]([/teal][navy]$res[/navy][teal][[/teal][purple]0[/purple][teal]][/teal] [teal]==[/teal] [navy]$password[/navy][teal])[/teal] [b]echo[/b] [green][i]"success"[/i][/green][teal];[/teal] [gray]/* or header */[/gray]
    [b]else[/b] [b]echo[/b] [green][i]"incorrect password?"[/i][/green][teal];[/teal]
  [teal]}[/teal] [b]else[/b] [b]echo[/b] [green][i]"incorrect name"[/i][/green][teal];[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top