<?php
require_once '../../library/config.php';
require_once '../library/functions.php';
require_once '../library/logged.php';
require_once '../../library/validation.php';

if (isset($_POST['field'])){
	$table = txt_only($_POST['table']);
	$id = isNumber(trim($_POST['id']));
	$field = txt_only($_POST['field']);
	$val = txt_only($_POST['val']);
	
	if ($table == 'cate'){
		mysql_query('UPDATE `cate` SET `'.$field.'` = "'.$val.'" WHERE `cat_id` = '.$id, $connePaper);
	}
	if ($table == 'subcate'){
		mysql_query('UPDATE `subcate` SET `'.$field.'` = "'.$val.'" WHERE `subcat_id` = '.$id, $connePaper);
	}
	mysql_close($connePaper);
}

if (isset($_POST['add_cate'])){
	$field = txt_only($_POST['input_field']);
	$value = txt_only($_POST['input_val']);
	
	$sql_rank = mysql_query('SELECT `rank` FROM `cate` ORDER BY `rank` DESC LIMIT 0, 1');
	$row_rank = mysql_fetch_assoc($sql_rank);
	$insert_rank = $row_rank['rank']+1;
	mysql_query("INSERT INTO `cate` ($field, rank) VALUES ('$value', '$insert_rank')", $connePaper);
}

if (isset($_POST['match_cat_id'])){
	$id = isNumber(trim($_POST['match_cat_id']));
	$value = txt_only($_POST['input_val']);
	$subcat_name = txt_only($_POST['subcat_name']);
	
	$sql_rank = mysql_query('SELECT `rank` FROM `subcate` WHERE `match_cat_id` = '.$id.' ORDER BY `rank` DESC LIMIT 0, 1');
	$row_rank = mysql_fetch_assoc($sql_rank);
	$insert_rank = $row_rank['rank']+1;
	
	mysql_query("INSERT INTO `subcate` (match_cat_id, $subcat_name, rank) VALUES ('$id', '$value', '$insert_rank')", $connePaper);
}
?>
