PHP login, getting wrong count value from query / fetch array

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-22T16:57:46Z Indexed on 2010/04/22 17:33 UTC
Read the original article Hit count: 159

Filed under:
|

Hello,

*EDIT*Thanks to the comments below it has been figured out that the problem lies with the md5, without everything works as it should.
But how do i implent the md5 then?

I am having some troubles with the following code below to login.
The database and register system are already working.
The problem lies that it does not find any result at all in the query.
IF the count is > 0 it should redirect the user to a secured page.
But this only works if i write count >= 0, but this should be > 0 , only if the user name and password is found he should be directed to the secure (startpage) of the site after login.
For example root (username) root (password) already exists but i cannot seem to properly login with it.

<?php
session_start();

if (!empty($_POST["send"]))
{   
$username = ($_POST["username"]);
$password = (md5($_POST["password"])); 
$count = 0;

$con = mysql_connect("localhost" , "root", "");

mysql_select_db("testdb", $con);

$result = mysql_query("SELECT name, password FROM user WHERE name = '".$username."' AND password = '".$password."' ")
    or die("Error select statement");


$count = mysql_num_rows($result);


if($count > 0) // always goes the to else, only works with >=0 but then the data is not found in the database, hence incorrect
{
    $row = mysql_fetch_array($result);

    $_SESSION["username"] = $row["name"];
    header("Location: StartPage.php");
}
else
{
    echo "Wrong login data, please try again";
}

mysql_close($con);    
}

?>

© Stack Overflow or respective owner

Related posts about php

Related posts about beginner