<?php
/*
* @author Istvan Franko
* @copyright 2014
*/

session_start();
if (isset($_SESSION['adminlogin'])){
	//user in logged in. Show a message on the screen
} else {
	//redirect the user to the login form.
	session_destroy();
	header('Location: index.php');
}
?>

<html>

<head>
<title>Natty Notebooks Inc.</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>


<div id="container">

	<div id="header">
	<! header picture in css >
	</div>
	
	<div id="navigation">
		<?php include 'admin-nav-bar.php'; ?>
	</div>
	

<div id="aside">
	<!--Link for navigation-->
	<a href="admin.php"><p style="text-align:center">Back to list</a></p>	
	
	<fieldset style="width:85%"><legend>HELP</legend>
	<p>You have to confirm deletion of this item.</p> 
	</fieldset><br>
</div>

<div id="content">


<?php

//check $id setted or not
if (!isset($id))
{
	echo "<h2>Sorry, there appears to have been an error.</h2><hr/>";
	echo "<a href='admin.php'><p style='text-align:center'>Back to home</a></p>";
}
else
{
	//get the ID from the query string.
	$id=$_GET['id'];

	if(!is_numeric($id))
	{ //check is a number.
		echo "<h2>Sorry, there appears to have been an error.</h2><hr/>";
		echo "<a href='admin.php'><p style='text-align:center'>Back to home</a></p>";
	}
	else
	{
	//sql connection details
	$con = mysql_connect("localhost","*****","*****");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("university", $con);

	$query=Mysql_query("SELECT * FROM natty WHERE ItemID=$id ");
	$row=mysql_fetch_object($query);
	echo mysql_error();

	//get the value of each column and add to local variables
	$ItemName = $row->ItemName;

	//close the connection to the database.
	mysql_close($con);
	}
}


// define variables and set to empty values
$confirmationErr = $confirmation = "";


if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   $id= test_input($_POST["id"]);
   $confirmation = test_input($_POST["confirmation"]);
   
  if (empty($_POST["confirmation"]))
    {$confirmationErr = "(Confirmation is required)";}
  else
    {$confirmation = test_input($_POST["confirmation"]);}
	
	if ($confirmation != "")
	{
	//confirmation check
	switch ($confirmation)
		{
		case "yes";
		$con = mysql_connect("localhost","*****","*****");
		if (!$con)
		{
		die('Could not connect: ' . mysql_error());
		}

		mysql_select_db("university", $con);



		$sql = "DELETE FROM natty WHERE ItemID='$id' ";
		echo $sql;

		if (!mysql_query($sql,$con))
		{
		die ('Error: ' . mysql_error());
		}

		//close the connection to the database.
		mysql_close($con);
	
		//redirect to home page
		header('Location: admin.php');
		break;

		case "no";
		//redirect to home page
		header('Location: admin.php');
		break;

		default;

		}
	}
	
}

function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}



?>

<br>
<fieldset style="width:95%"><legend>DELETING</legend>
	
<p> Please confirm you want to delete? </p>

<?php

//show item name 
if (isset($ItemName))
{
echo "<h2>" . $ItemName . "</h2>"; 
}
?>

<p><span class="error">* required field.</span></p>
<form  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<form>
Confirmation:<span class="error">*</span><br>
<input type="hidden" name="id" value="<?php echo $id;?>">
<input type="radio" name="confirmation" value="yes">Yes<br>
<input type="radio" name="confirmation" value="no">No<br>
<span class="error"> <?php echo $confirmationErr;?> </span>

<input type="submit" name="Submit" value="Confirm"><br>
</form> 


</fieldset>
</div>

<div id="footer">
<p align="center">Copyright &#169; Istvan Franko, 2014</p>
</div>


</div>
</body>
</html>
