* I had just posted this on the DB forum, but just realized its more appropriately posted here, sorry , not trying to spam!*
Help! Simple Authentication from SAMS book not working.
Ok, I have built a little different table than in the Sams Ch14 example.
This is what I have
username | fname | lname | pass |
rgardner | Richard|Gardner| abc123|
The script is as follows below:
The meat, err, the count(*) works as intended when i input in mysql command line
mysql> select count(*) from users where username='rgardner' and pass = 'abc123';
I get as expected:
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
but when run in the script, the output runs from the last "else" break. I put in the break code 'echo "<br>".$count';
and that returns = 0
what gives?
<?
if (!isset($name) && !isset($password)){
?>
<h3> Please Log In</h3>
This page is secret.
<form method = post action = "secretdb.php">
<table border=1 bgcolor=gray>
<tr>
<th> Username</th>
<td><input type=text name=name></td>
</tr>
<tr>
<th> Password </th>
<td><input type=text name=password></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type= submit value= "Log In">
</td>
</tr>
</table
</form>
<?
}
else {
// connect to mysql
@ $mysql = mysql_connect(localhost, "booker", abc123);
if(!$mysql)
{
echo 'cannot connect to db';
exit;
}
$mysql = mysql_select_db('auth');
if(!$mysql)
{
echo 'cannot Select db';
exit;
}
// query the db to see if there is a record that matches
$query = "select count(*) from users where
username = '$username' and
pass = '$pass'";
$result = mysql_query( $query );
if(!$result)
{
echo 'Cannot run Query';
exit;
}
$count = mysql_result($result,0,0);
if ($count > 0)
{
echo "Welcome";
}
else
{
//visitor's name and pass combo are not corect
echo "Sorry that user/pass combo is not correct.<br>";
echo "Please try again";
echo "<br>".$count;
}
}