<?php
session_start();
$username = $_SESSION['membership'];
include("connect.php");
$ISBN = $_POST["ISBN"];
$comment = $_POST["bookcomment"];
$date = date("Y-m-d H:m:s"); 

if($_POST["Submit"] == "Submit")
	{
	$result2 = mysql_query("SELECT * FROM bookmessage WHERE ISBN = '$ISBN' && memLname = '$username'");
	$num_results2 = mysql_num_rows($result2);
	
	if($num_results2==1){
	echo "<script language='javascript'>alert('You only can comment once per book.')</script>";
	echo "<script language='javascript'>window.location='bookviewpage.php?ISBN=$ISBN'</script>"; 
	}else{
	$result = mysql_query("INSERT INTO bookmessage(ISBN,memLname,comment,time)
	VALUES
	('$ISBN','$username','$comment','$date')");
	$db = mysql_close();
	echo "<script language='javascript'>alert('Thanks for your comment.')</script>";
	echo "<script language='javascript'>window.location='bookviewpage.php?ISBN=$ISBN'</script>";
	}
	}
	
if($_POST["Delete"] == "Delete")
	{
	$result3 = mysql_query("delete from bookmessage where ISBN='$ISBN' && memLname = '$username'");
	$db = mysql_close();
	echo "<script language='javascript'>alert('Your comment is deleted.')</script>";
	echo "<script language='javascript'>window.location='bookviewpage.php?ISBN=$ISBN'</script>";
	}
?>