User name and password

I am working on a school assignment in which the user needs to register on mysql before they can enter the site. With this code it keeps telling me that the name and password are not registered in the database, when they are. It keeps looping in the if statement and never makes it to the else part.

Thanks in advance

[php]<?php
session_start();
mysql_connect(“localhost”,“root”,"") or die(mysql_error());
mysql_select_db(“data”) or die(mysql_error());

$fname = $_POST[“fname”];
$password = $_POST[“password”];
//$image = $_POST[“imagename”];

$sql = “SELECT * FROM users where name=’”.$fname."’ and password=’".$password."’";
//echo $sql;
$result = mysql_query($sql);
//echo $result;

if(!$result){
echo “it is not there”;
?>

<?php

}
else{
echo “your here”;
$_SESSION[‘name’]=$fname;
?>

<?php } ?>[/php]

in if condition you are checking the mysql_query result which only gives you query successfully executed or not and you have to check the data that the user is registered or not so you have to fetch the data or count the data and on the basis of that you have to check that in conditions see my codes below after the mysql_query() i have fetched the data and check in conditions.

<?php session_start(); mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("data") or die(mysql_error()); $fname = $_POST["fname"]; $password = $_POST["password"]; //$image = $_POST["imagename"]; $sql = "SELECT * FROM users where name='".$fname."' and password='".$password."'"; $result = mysql_query($sql); $data = mysql_fetch_array($result); if(!$data){ echo "it is not there"; ?> <?php } else{ echo "your here"; $_SESSION['name']=$fname; ?> <?php } ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service