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

if( !isset($_SESSION['userid']))
{  echo "username is not set <br>";
}
else
{  echo "username is set : ".$_SESSION['userid']."<br>";	
}
$bid = $_POST['bid'];
$sid = $_POST['sid'];
$isbn = $_POST['isbn'];
$stype = $_POST['stype'];
$price = $_POST['price'];
$date  = $_POST['date'];
$ccnum  = $_POST['ccnum'];
$cctype  = $_POST['cctype'];
$ccexpiry  = $_POST['ccexpiry'];
?>

<!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>Transaction Status</title>

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


//finding max tid
$query = "SELECT max(TID) as num from TRANSACTION;";
$result = mysql_query($query);
$maxtid = mysql_fetch_array($result);
if ( $maxtid['num']==NULL)
$tid = 1;
else
$tid = $maxtid['num']+1;


$query = sprintf("insert into TRANSACTION values('%d','%s','%d','%d','%s','%s');",$tid,$date,$price,$ccnum,$cctype,$ccexpiry);
echo $query.'<br>';				  
if (!mysql_query($query,$connect))
{
die('Error in inserting transaction: ' .mysql_error());
}

$query = sprintf("insert into BUYSELLRECORD values('%d','%s','%s','%s','%s');",$tid,$isbn,$sid,$bid,$stype);
echo $query.'<br>';				  
if (!mysql_query($query,$connect))
{
die('Error in inserting transaction: ' .mysql_error());
}

// decreasing the number of copies of seller
$query = sprintf("UPDATE SELLER SET NUM_COPIES = NUM_COPIES-1 where SELLER_ID='%s' AND ISBN='%s' AND STYPE='%s' ;",$sid, $isbn, $stype);
echo $query.'<br>';				  
if (!mysql_query($query,$connect))
{
die('Error in inserting transaction: ' .mysql_error());
}

echo "Transaction successfully added with transaction ID : ".$tid;
?>

</body> 
</html>
