<?php
session_start();
$username = $_SESSION['membership'];
$temItem = $_POST["temItem"];
$temQuantity = $_POST["temQuantity"];
$oQuantity = $_POST["originalQuantity"];

	include("connect.php");

	$result4 = mysql_query("SELECT * FROM item WHERE ISBN = '$temItem'");
	$row = mysql_fetch_array($result4);
	$oldQuantity = $row[itemQuantity];
	$oldSales = $row[itemSales];
	$itemDate = $row["itemDate"];
	$date = date("Y-m-d");
	
if(date("Y-m-d",strtotime($date))>date("Y-m-d",strtotime($itemDate))){
	$newQuantity = $oldQuantity + $oQuantity - $temQuantity;
	$newSales = $oldSales - $oQuantity + $temQuantity;
	$result2 = mysql_query("UPDATE item SET itemQuantity = '$newQuantity', itemSales = '$newSales' WHERE ISBN = '$temItem'");
	
	$result = mysql_query("UPDATE temcart SET temQuantity = '$temQuantity' WHERE temID = '$username'&&temItem = '$temItem'");
}else{
	$oldSales = $oldSales - $oQuantity;
	$newSales = $oldSales + $temQuantity;
	$result2 = mysql_query("UPDATE item SET itemSales = '$newSales' WHERE ISBN = '$temItem'");

	$result = mysql_query("UPDATE temcart SET temQuantity = '$temQuantity' WHERE temID = '$username'&&temItem = '$temItem'");
}
	$db = mysql_close();
	echo "<script language='javascript'>alert('Item enter into cart.')</script>";
	echo "<script language='javascript'>window.location='subtemcart.php'</script>";
	
?>