<?php session_start();
$previous = $_SESSION['previous'];
$_SESSION['previous'] = "login";
echo $previous.'<br>';
?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LOGIN</title>
</head>

<body>
<?php

$connect = mysql_connect("localhost","root","programming");
if (!$connect)
  {	die('Could not connect: ' . mysql_error());  }
  
mysql_select_db("BOOK_DB", $connect);
$query = sprintf("SELECT USER_ID from PROFILE WHERE USER_ID='%s';", $_POST['userid']);
$result = mysql_query($query);
if(mysql_num_rows($result)==0)
  {
	  $_SESSION['error'] = 'userid';
	  	if ($previous == "buy")		
		echo '<script type="text/javascript"> document.location.href = "buy.php?"; </script>';
		else
		echo '<script type="text/javascript"> document.location.href = "home.php?"; </script>';
  }
else
  {
	$query = sprintf("SELECT USER_ID from PROFILE WHERE USER_ID='%s' AND PASSWORD='%s' ;", $_POST['userid'], $_POST['password']);
	$result = mysql_query($query);
	if(mysql_num_rows($result)==0)
	  {
		$_SESSION['error'] = 'password';
		if ($previous == "buy")		
		echo '<script type="text/javascript"> document.location.href = "buy.php?"; </script>';
		else
		echo '<script type="text/javascript"> document.location.href = "home.php?"; </script>';
	  }
	else
	  {
		 $_SESSION['userid'] = $_POST['userid'];
		 echo $previous;
		 if ($previous == "buy")
		 echo '<script type="text/javascript"> document.location.href = "search_display.php?"; </script>';
		 else
		 echo '<script type="text/javascript"> document.location.href = "home.php?"; </script>';
	  }
   }
?>
</body>
</html>
