Hi guys, can someone assist me – I don’t understand why every time I run this code the error msg “Please enter a username and password! ” appear before I type anything.
login.php
[code=php]
<?php
$username = (isset($_POST[‘username’])) ? $_POST[‘username’] : ”;
$password = (isset($_POST[‘password’])) ? $_POST[‘password’] : ”;
if($username && $password) {
$connect = mysql_connect(“localhost”, “root”, “”) or die (“Couldn’t connect!”);
mysql_select_db(“friendsdb”) or die (“Couldn’t find the DB”);
$query = mysql_query (“SELECT * FROM `user` WHERE username = ‘$username'”);
$numrows = mysql_num_rows($query);
if ($numrows != 0){
while ($row = mysql_fetch_array($query)) {
$dbusername = $row[‘username’];
$dbpassword = $row[‘password’];
}
//Check to see if they are match!
if ($username == $dbusername && md5($password) == $dbpassword) {
echo “You are in! <a href = ‘member.php’> Click </a> here to enter the member page!”;
$_SESSION[‘username’] = $username;
}
else
echo “Incorrect password!”;
//code of login
}else
echo “That user does not exist!”;
//echo $numrows;
}
else
echo “Please enter a username and password!”;
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head>
<body>
<form action=”<?php echo $_SERVER[‘PHP_SELF’];?>” method=”post”>
<!–form action=”login.php” method=”post”–>
Username: <input type=”text” name=”username” /><br />
Password: <input type=”password” name=”password” /><br />
<input type=”submit” value=”Log in” />
</form> <p>
<a href=”register.php”>Register?</a>
</body>
</html>