Когда у вас возникают проблемы со сценарием, иногда проще, если вы удалите весь лишний пух. Попробуйте код ниже. Я немного прибрался.
Я не проверял это, но все еще думаю, что это шаг вперед.
<?php
session_start();
/* If the form has been submitted. */
if (!empty ($_POST))
{
/* If there is not missing data. */
if (empty ($_POST['username']) && empty ($_POST['password']))
{
/* Connect to the database server. */
$connection = mysql_connect ("localhost", "root") or die ("Error: can not connect to the database.");
/* Select the database. */
mysql_select_db ("login") or die ("Error: Can not select the database.");
/* Make the query. */
$login_check = mysql_query ("SELECT * FROM users WHERE username = '" . mysql_escape_string ($_POST['username']) . "' AND password = '" . mysql_escape_string ($_POST['password']) . "' LIMIT 1") or die ("MySQL query error.");
/* If there is a row returned. */
if (mysql_num_rows ($login_check) > 0)
{
echo 'The username and password have matched.';
exit;
}
/* There were no rows returned. */
else
{
$_SESSION['login_error'] = 'Incorrect username or password.';
}
}
/* There's missing data. */
else
{
$_SESSION['login_error'] = 'Both the username and password are required.';
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing Login Form</title>
</head>
<body>
<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>User name: <input type="text" name="username" id="username"></p>
<p>Password: <input type="password" name="password" id="password"></p>
<p><input type="submit" name="login" value="Login"></p>
<?php
if (!empty ($_SESSION['login_error']))
{
echo '<p>', htmlspecialchars ($_SESSION['login_error'], ENT_QUOTES), '</p>';
unset ($_SESSION['login_error']);
}
?>
</form>
</body>
</html>
Пожалуйста, примите во внимание все, что набрал Роборг. Это очень хороший совет.