<?php
session_start();


include("connect.php");

$poNo = $_POST['poNo'];
$j = $_POST['counter'];
$qtotal = $_POST['qtotal'];
$total = $_POST['total'];

	$result3 = mysql_query("UPDATE purchaseorder SET poTotal = '$total', qTotal = '$qtotal' WHERE poNo = '$poNo'");
	

for ($k=0; $k <$j; $k++)
{
$ISBN= $_POST['ISBN'.$k];
$quantity = $_POST['quantity'.$k];
$price = $_POST['price'.$k];

	$result = mysql_query("SELECT poQuantity FROM poitem WHERE poItem = '$ISBN'");
	
	$row = mysql_fetch_array($result);
	$num_results = mysql_num_rows($result);

if(($quantity<1)||($price<0)){
echo "<script language='javascript'>alert('Please check your amount and unit price.')</script>";
echo "<script language='javascript'>window.location='addstock.php?po=$poNo'</script>";
}else{
	$result2 = mysql_query("UPDATE poitem SET poQuantity = '$quantity', poPrice = '$price' WHERE poItem = '$ISBN'&&poNo = '$poNo'");
	
}
}
	$db = mysql_close();
	echo "<script language='javascript'>alert('Purchase order $poNo is created')</script>";
	echo "<script language='javascript'>window.location='addpo.php'</script>";
?>