<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

$catid  = $_GET["catid"];
$check_cat_sql = "select category_id from ".$tableprefix."categories where parent_id = $catid";
$check_cat_rs  = mysql_query($check_cat_sql);
if(mysql_num_rows($check_cat_rs))
{
   header("Location:catalog.php?mesgg=Deletion cannot be done since there are subcategories under the category.");
   exit;
}else if(!mysql_num_rows($check_cat_rs))
{
  $check_pdt_sql = "select product_id from ".$tableprefix."products where product_category = $catid";
  $check_pdt_rs  = mysql_query($check_pdt_sql);
  
  if(mysql_num_rows($check_pdt_rs))
  {
    header("Location:catalog.php?mesgg=Deletion cannot be done since there are products under the category.");
    exit;
  }
}
if(!mysql_num_rows($check_cat_rs) && !mysql_num_rows($check_pdt_rs))
{
	mysql_query("DELETE FROM ".$tableprefix."categories WHERE `category_id` = ".$catid ) or die(mysql_error());
        header("Location:catalog.php?message=".MSG_CATEGORY_DELETED_SUCCESSFULLY);
        exit;
}
?>