<?php
/*
This will allow administrator to edit and delete informaiton of the officers. 
*/
/*
	Restrict other user from accessing this page
*/
include("../includes/mysql_login.php");
session_start();
if(!isset($_SESSION['autheticate']))
{
	header("Location: ../index.php");
	exit;
}
if($_SESSION['admin'] == 0)
{
	header("location: ../index.php");
	exit;
}
$link = $_SERVER['PHP_SELF'];
function queryOfficers()
	{
		$query_officers = mysql_query("SELECT * FROM `officers`") or die("Unable to query table officers");
		echo "<table width=94%  border=5 cellspacing=5>";
		echo "<tr>";
		echo "<td><b> ID<b></td> <td><b> First Name </b></td> <td> <b>Last Name </b></td> <td> <b>Position </b></td> <td><b> Photo Uploaded</b></td><td><b> Action </b></td>";
		echo "</tr>";
		while($row_query_officers = mysql_fetch_array($query_officers))
		{
			echo "<tr>";
			echo "<td>";
			echo $row_query_officers['officersid'];
			echo "</td>";
			echo "<td>";
			echo $row_query_officers['first_name'];
			echo "</td>";
			echo "<td>";
			echo $row_query_officers['last_name'];
			echo "</td>";
			echo "<td>";
			echo $row_query_officers['position'];
			echo "</td>";
			echo "<td>";
			$row_query_officers['attachment'];
				if($row_query_officers['attachment'] == NULL)
				{
					echo "NO";
				} else
				{
					echo "YES";
				}
			echo "</td>";
			echo "<td>";
			echo "<a href = $linke?which=$row_query_officers[officersid]&what=edit>Edit </a> || ";
			echo "<a href = $linke?which=$row_query_officers[officersid]&what=delete>Delete </a> || ";
			echo "</td>";
			echo "</tr>";
		}
		echo "</table>";
	}
queryOfficers();
if(($_GET['what']==edit) && (!isset($_POST['edit'])))
{
	echo $id = $_GET['which'];
	echo "edit the officers row <br>";
	$query_officers = mysql_query("SELECT * FROM `officers` WHERE officersid='$id'") or die ("Can not query table officers");
	$row_query_officers = mysql_fetch_array($query_officers);
	echo $row_query_officers['first_name'];
	/*
		Form with initial values except attached photo information
	*/
	echo "<form action=\"$PHP_SELF\" enctype=\"multipart/form-data\" method=\"post\">";
  	echo "<p>First Name: 
    <input name=first_name type=text value=$row_query_officers[first_name]> 
  	</p>";
  	echo "<p>Last Name: 
    <input name=last_name type=text value=$row_query_officers[last_name]> 
  	</p>";
  	echo "<p>Position: 
    <input name=position type=text value=$row_query_officers[position]>
  	</p>";
  	echo "<p>  Upload File:
    <input type=file name=uploadedfile>
	</p>";
  	echo "<p>
    <input type=submit name=edit value=Edit>
	</p>";
	echo "</form>";
}
if(isset($_POST['edit']))
{	
	$id = $_GET['which'];
	$first_name = $_POST['first_name'];
	$last_name = $_POST['last_name'];
	$position = $_POST['position'];
	include("../includes/fileupload.php");
	 $filename = $filename;
	 echo $filename. " Is the filename <br>";
	 $update_officers = mysql_query("UPDATE officers SET first_name='$first_name', last_name='$last_name', position='$position', attachment='$filename' WHERE officersid='$id'")
	 					or die("Fail update officer's information");
	echo "<meta http-equiv = \"refresh\" content = \"1; URL=editOfficers.php\">";

	 
}
if($_GET['what']==delete)
{
	$id = $_GET['which'];
	$delete_officers = mysql_query("DELETE FROM officers WHERE officersid='$id'") or die ("Faile to delete row");
	echo "<meta http-equiv = \"refresh\" content = \"1; URL=editOfficers.php\">";
}
?>
<a href="adminform.php">Admin Home</a>||<a href="../index.php">Home</a>