-1

I have a html page that sends value pass to log.php :

<?php 
$PHONE = $_POST['pass'];

mysql_connect('host.com', 'user', 'pass'); mysql_select_db('userdb');  

$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PASS='$PASS'",
mysql_real_escape_string($PASS))) or die(mysql_error()); while($row = mysql_fetch_assoc($results))
{ $rows[1] = $row; }
echo "Welcome ";
echo "$rows[1][FNAME]." ".$rows[1][LNAME]"
echo " you are logged in successfully"

?>

I get the result in log.php file in this form when value 'pass' is not being registered in database:

Welcome [//here user's first & last name if registered or remains blank] you are logged in successfully

I have table 'details' created

FNAME-First name
LNAME-Lastname
BIRTHDAY-Birthday
PHONE- user's no.
PASS- user's password.

My Question is , I want to just display ' You are not registered user' when pass value is not been registered in the database.Any help would be greatfull ?

3 Answers3

1

Just check if you actually get a record or not using mysql_num_rows,

$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PASS='$PASS'",
           mysql_real_escape_string($PASS))) or die(mysql_error());
if(mysql_num_rows($results) > 0){
   while($row = mysql_fetch_assoc($results))
   { 
      $rows[1] = $row; 
   }
   echo "Welcome ";
   echo $rows[1]['FNAME']." ".$rows[1]['LNAME'];
   echo "You are logged in successfully";
}
else{
    //Redirect them back with error message
    header("Location: http://www.example.com/index.php?err='You are not registered user'");
}

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Parse error: syntax error, unexpected '"', expecting ',' or ';' in /home/a8550571/public_html/Log.php file, its seems somethings going wrong in your code. – user2513330 Jun 25 '13 at 12:51
  • I direct you how you can pass data to other page. You can solve a parse error by yourself , debuging the same. Btw I have update my answer `echo $rows[1]['FNAME']." ".$rows[1]['LNAME'];` you had a syntax error near this lines. – Rikesh Jun 25 '13 at 12:53
0
if(empty($rows[1][FNAME])){
    echo 'You are not registered user';
}
else{
    echo "Welcome ";
    echo "$rows[1][FNAME]." ".$rows[1][LNAME]";
    echo " you are logged in successfully";
}
stackErr
  • 4,130
  • 3
  • 24
  • 48
0

You took value in $phone variable for password and in select query used PASS= $PASS so how can it run?

Change this then run like below:

$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PASS='$PHONE'",
mysql_real_escape_string($PHONE))) or die(mysql_error());
Daanvn
  • 1,254
  • 6
  • 27
  • 42