<?php
session_start();
include "../functions.php";

$sessionID = session_id();

checkAdmin($sessionID);

if ($_POST["submit"] == 1)
{
	$categoryName = $_POST["categoryName"];
	$categoryDescrip = $_POST["categoryDescrip"];

	$query = "select categoryID from news_category where categoryName='$categoryName'";
	$result = mysql_query($query) or die (mysql_error());
	if (trim($categoryName) == "")
	{
		$err = "categoryName cannot be empty";
	}
	elseif (mysql_num_rows($result) > 0)
	{
		$err = "there is already a category called $categoryName";
	}

	if (! $err)
	{
		$query = "insert into news_category (categoryName, categoryDescrip) values('$categoryName', '$categoryDescrip')";
		mysql_query($query) or die (mysql_error());
		header ("location: category.php");
	}
}
?>

<?php
$title = "Add News Category";
include "header.php";
?>
<tr>
<td width=100%>
  <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
  <table width=100% border=0 cellpadding=5>
    <tr>
      <td colspan=2 align="center">
        <font color="red">
        <?php
        echo $err . "&nbsp;";
        ?>
        </font>
      </td>
    </tr>
    
    <tr>
      <td align="right">Category Name:</td>
      <td><input type="text" name="categoryName" value="<?php echo $categoryName; ?>" size=30></td>
    </tr>
    
    <tr>
      <td align="right" valign="top">Category Description:</td>
      <td><textarea name="categoryDescrip" rows=5 cols=25 wrap="virtual"><?php echo $categoryDescrip; ?></textarea></td>
    </tr>
    
    <tr>
      <td align="center" colspan=2><input type="hidden" name="submit" value=1><input type="submit" value=" Add ">&nbsp;<input type="reset" value="Cancel"></td></form>
    </tr>
  </table>
</td>
</tr>
<?php
include "footer.php";
?>