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

if( !isset($_SESSION['userid']))
{ echo "username is not set <br>";
}
else
{ echo "username is set : ".$_SESSION['userid']."<br>";	
}

$isbn = $_POST['isbn'];
$title = $_POST['title'];
$lang = $_POST['language'];
$publisher = $_POST['publisher'];
$author = $_POST['author'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Results </title>

<script type="text/javascript">
function validate(form)
{
form.submit()
}

function re(form1)
{ 
  if(confirm("Do you want to reset the form"))
  {
  }   
}
</script>


<?php

if (isset($_SESSION['userid']))
{
echo "<h2> Displaying all possible sellers of the selected book for isbn :". $isbn. "</h2><br>";

$connect = mysql_connect("localhost","root","programming");
if (!$connect)
{ die('Could not connect: '.mysql_error());  }
mysql_select_db("BOOK_DB", $connect);

 
// Displaying selling options for the same book
$query = "SELECT
		  BOOK.ISBN as isbn, BOOK.TITLE as title, SELLER_ID as sid, 
		  ADDRESS as uadr, STYPE as stype, NUM_COPIES as num, PRICE as price
		  from BOOK, SELLER, PROFILE
		  WHERE BOOK.ISBN=SELLER.ISBN 
		  AND SELLER.SELLER_ID= PROFILE.USER_ID";

// num of copies would be decreased after selling each copy
if ($isbn!="")
	$query=$query.sprintf(" AND BOOK.ISBN='%s'",$isbn);
if ($title!="")
	$query=$query.sprintf(" AND BOOK.TITLE like '%%%s%%'",$title);
if ($lang!="")
	$query=$query.sprintf(" AND BOOK.LANG='%s'",$lang);
if ($publisher!="")
	$query=$query.sprintf(" AND BOOK.PUBLISHER like'%%%s%%'",$publisher);
	
$query=$query.';';
echo $query.'<br>';
				  
$result = mysql_query($query);
echo mysql_num_rows($result);

  echo "<table>
	<tr>
	<th></th>
	<th>TITLE</th>
	<th>SELLER ID</th>
	<th>SELLER ADDRESS</th>
	<th>BOOK TYPE</th>
	<th>NUM COPIES</th>
	<th>PRICE</th>
	<th></th>
	</tr>";

$i = 1;
while($row = mysql_fetch_array($result))
  {
  echo sprintf('<form id="form%s" name="form%s" method="post" action="buy_validate.php">',$i,$i); 
  
  echo "<tr>";
  echo sprintf('<td> <input type="hidden" name="isbn" id="isbn" readonly="readonly" value="%s"></input> </td>',$row['isbn']);
  echo sprintf('<td> <input type="text" name="title" id="title" readonly="readonly" value="%s"></input> </td>',$row['title']);
  echo sprintf('<td> <input type="text" name="sid" id="sname" readonly="readonly" value="%s"></input> </td>',$row['sid']);
  echo sprintf('<td> <input type="text" name="uadr" id="uadr" readonly="readonly" value="%s"></input> </td>',$row['uadr']);
  echo sprintf('<td> <input type="text" name="stype" id="stype" readonly="readonly" value="%s"></input> </td>',$row['stype']);
  echo sprintf('<td> <input type="text" name="num" id="num" readonly="readonly" value="%s"></input> </td>',$row['num']);
  echo sprintf('<td> <input type="text" name="price" id="price" readonly="readonly" value="%s"></input> </td>',$row['price']);
  echo sprintf('<td> <input type="submit" name="button" id="button" value="Confirm" />');
  echo "</tr>";
  echo "</form>";
  $i=$i+1;
 }  
echo "</table>"; 
}

else
{
	 $s1="userid";
	 $s2="password";
	 if (isset($_SESSION['error']))
	 {
		if ($_SESSION['error']=="userid")
		{ 
		 $s1 = $s1."(invalid username)";
		 unset($_SESSION['error']);
		}
		else	 
		if ($_SESSION['error']=="password")
		{ 
			$s2 = $s2." (invalid password)";
			unset($_SESSION['error']);
		}
	 }
  echo
	  '<form id="form1" name="form1" method="post" action="login.php">
	  <label>'.$s1.'
	  <input type="text" name="userid" id="userid" value="ashgupta" />
	  </label>
	  <br>
	  <label>'.$s2.'
	  <input type="text" name="password" id="password" value="great" />
	  </label>
	  <br>
	  <p>
		<input type="submit" name="button" id="button" value="Submit" />
	  </p>
	  </form>
	  
	  <br><br> Do not have an id, then <a href="register.php">Register</a> <br><br>
	  Go back to search Results Page <a href="search_display.php">Search Results</a> <br><br>';
}
?>

</body> 
</html>
