I'm trying to make a simple login screen with php, but every time I submit the form(with correct values) I am returned to the Login page with the error "Invalid login, try again" shown.

I can't think of any reason why this will not work, the variables are correct as is the database, table and elements.

<?php
require_once("db_connect.php");
session_start();
if(isset($_REQUEST['submit'])){

   // Check for alphanumeric values
	$regExpression = "^[a-zA-Z0-9]+$";
	$uname  = ereg( $regExpression, $_REQUEST['username'])? $_REQUEST['username'] : "";
	$psswd1 = ereg( $regExpression, $_REQUEST['password'])? $_REQUEST['password'] : "";
	// now try to authenticate these values

	// Query database to see if the name and password are valid
	$db_link = db_connect("forumch");
	$query = "SELECT * FROM users WHERE username='$uname' "
            ." AND password = '$psswd1'";
	$result  = mysql_query($query);



	$count=mysql_num_rows($result);
	if($count==1){
      $_SESSION['valid_user'] = $uname;
      $_SESSION['authenticated'] = true;
      mysql_free_result($result);
      mysql_close();
	  display_members_page();   // function
	}
	else{
      display_login_page("Invalid login, try again");
    }
}
else // first time so display form to login
{
   display_login_page("Please log in");
}
?>

I think the problem may be these two lines: between lines 17 and 25
$count=mysql_num_rows($result);
if($count==1){
But I've been looking at this for a couple of hours now and I still can't get it.
Here is the remainder of my code:

<?php
function display_login_page($message)
{
?>
<html>
<head><title>Members Login Page</title></head>
<body>
<h1>Login Page</h1>
<h2><?php echo $message ?></h2>
<form method="POST">
<table border="1">
<tr><td>
   <table border="0">
   <tr><td>User Name:</td>
   <td><input type="text" name="userid"></td</tr>
   <tr><td>Password:</td>
   <td><input type="password" name="password"></td>
   </tr>
   <tr><td colspan=2 align=center>
   <input type="submit" name="submit" value="Log in"></td></tr>
   </table>
</td></tr>
</table>
</form></body></html>
<?php
}
?>

<?php
function display_members_page()
{
?>
<html><head><title>Members Page</title></head>
<body>
You have successfully logged in as user
<strong><?php echo $_SESSION['valid-user']?></strong>
<p>
<a href="members.php?<?php echo SID?>">Member
pages</a><br>
<a href="logout.php?<?php echo SID?>">Logout</a>
</p>
</body></html>
<?php } ?>

Thanks.

Never mind. The name of the first form field was wrong. Should have been "username".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.