<?php
	session_start();
	if (!isset($_SESSION['view_id']))
	{
		header('Location: index.php');
		exit;
	}

	$view_id = $_SESSION['view_id'];
	
	$products_id = $_GET['products_id'];

	include "../connect/db_connection_2.php";
	
	//Get paths to images to delete.
	$sql = "SELECT * FROM zen_products WHERE `products_id` = $products_id";
	$ret = mysql_query($sql);
	$row = mysql_fetch_assoc($ret);
	$master_categories_id = $row["master_categories_id"];
	$manufacturers_id = $row["manufacturers_id"];
	$products_image = $row["products_image"];
	$thumbnail_image = $row["thumbnail_image"];
	
	$sql = "SELECT * FROM zen_categories_description WHERE `categories_id` = $master_categories_id";
	$ret = mysql_query($sql);
	$row = mysql_fetch_assoc($ret);
	$categories_name = $row["categories_name"];
	$categories_folder = str_replace(" ", "_", $categories_name);
	
	$sql = "SELECT * FROM zen_manufacturers WHERE `manufacturers_id` = $manufacturers_id";
	$ret = mysql_query($sql);
	$row = mysql_fetch_assoc($ret);
	$image_folder = $row["image_folder"];
	
	//Got stuff for finding images, now delete records.
	$sql = "DELETE FROM zen_products WHERE `products_id` = $products_id";

	if (!($ret = mysql_query($sql)))
		die("Error: Could not delete");
		
	$query = "DELETE FROM `zen_products_description` WHERE `products_id`=$products_id";
	mysql_query($query);
	
	
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	$file_name = $document_root . "/inventory_images/$categories_folder/$image_folder/$products_image";
	if (file_exists($file_name))
		rmdir($file_name);
	$file_name = $document_root . "/inventory_images/$categories_folder/$image_folder/$thumbnail_image";
	if (file_exists($file_name))
		rmdir($file_name);
	
	header("Location: manage_inventory.php?Deleted=1");
	
?>
