<?php
if ($_POST ['field_name'] && $_POST ['table'] && $_POST ['val'] && $_POST ['checkvalue']) {
	$table = $_POST ['table'];
	$fieldname = $_POST ['field_name'];
	$fieldname = explode ( "_", $fieldname );
	// print_r($fieldname);
	if ($fieldname [2] != '') {
		$fieldname = $fieldname [1] . '_' . $fieldname [2];
	} else {
		$fieldname = $fieldname [1];
	}
	
	$value = $_POST ['val'];
	$check = $_POST ['checkvalue'];
	if ($check == 'value') {
		$count = $db->get_count ( $table );
		
		if ($count == 1) {
			$update = $db->update ( $table, array ($fieldname => $value ), array ("id" => 1 ) );
		} else {
			$insert = $db->insert ( $table, array ($fieldname => $value ) );
		}
		
		if ($update || $insert) {
			echo 1;
		} else {
			echo 2;
		}
	} else {
		$count = $db->get_count ( $table );
		
		if ($count == 1) {
			$total_first = $db->run ( "show columns from `$table`" );
			$value1_first = $db->get_row ( "$table", array ("id" => 1 ) );
			$arr = array ();
			foreach ( $total_first as $row ) {
				$string = $value1_first [$row ['Field']];
				// type-checking comparison operator is necessary
				if (strpos ( $string, '%' ) !== false) {
					$arr [] = $string;
				}
			}
			
			$total = array_sum ( $arr );
			// ********************************second table data
			$countsecond = $db->get_count ( "second_salaryslip" );
			if ($countsecond == 1) {
				$total_second = $db->run ( "show columns from `second_salaryslip`" );
				$value1_second = $db->get_row ( "second_salaryslip", array ("id" => 1 ) );
				$arr1 = array ();
				foreach ( $total_second as $row ) {
					$string = $value1_second [$row ['Field']];
					// type-checking comparison operator is necessary
					if (strpos ( $string, '%' ) !== false) {
						$arr1 [] = $string;
					}
				}
				
				$total1 = array_sum ( $arr1 );
			}
			
			if ($countsecond == 1) {
				$totalvalue = $total + $value + $total1;
			} else {
				// after adding the value
				$totalvalue = $total + $value;
			}
			
			if ($totalvalue < 100) {
				$update = $db->update ( $table, array ($fieldname => $value ), array ("id" => 1 ) );
				echo 1;
			} else {
				echo 2;
			}
		
		} else {
			$insert = $db->insert ( $table, array ($fieldname => $value ) );
			echo 1;
		}
	
	}
} elseif (isset ( $_POST ['field_name_deduction'] ) && isset ( $_POST ['table_deduction'] ) && isset ( $_POST ['val_deduction'] ) && isset ( $_POST ['checkvalue_deduction'] )) {
	$table = $_POST ['table_deduction'];
	$fieldname = $_POST ['field_name_deduction'];
	$fieldname = explode ( "_", $fieldname );
	// print_r($fieldname);
	if ($fieldname [2] != '') {
		$fieldname = $fieldname [1] . '_' . $fieldname [2];
	} else {
		$fieldname = $fieldname [1];
	}
	$value = $_POST ['val_deduction'];
	$check = $_POST ['checkvalue_deduction'];
	if ($check == 'value') {
		$count = $db->get_count ( $table );
		
		if ($count == 1) {
			$update = $db->update ( $table, array ($fieldname => $value ), array ("id" => 1 ) );
		} else {
			$insert = $db->insert ( $table, array ($fieldname => $value ) );
		}
		
		if ($update || $insert) {
			echo 1;
		} else {
			echo 2;
		}
	} else {
		$count = $db->get_count ( $table );
		
		if ($count == 1) {
			$total_first = $db->run ( "show columns from `$table`" );
			$value1_first = $db->get_row ( "$table", array ("id" => 1 ) );
			$arr = array ();
			foreach ( $total_first as $row ) {
				$string = $value1_first [$row ['Field']];
				// type-checking comparison operator is necessary
				if (strpos ( $string, '%' ) !== false) {
					$arr [] = $string;
				}
			}
			
			$total = array_sum ( $arr );
			// ********************************second table data
			$countsecond = $db->get_count ( "first_salaryslip" );
			if ($countsecond == 1) {
				$total_second = $db->run ( "show columns from `first_salaryslip`" );
				$value1_second = $db->get_row ( "$table", array ("id" => 1 ) );
				$arr1 = array ();
				foreach ( $total_second as $row ) {
					$string = $value1_second [$row ['Field']];
					// type-checking comparison operator is necessary
					if (strpos ( $string, '%' ) !== false) {
						$arr1 [] = $string;
					}
				}
				
				$total1 = array_sum ( $arr1 );
			}
			
			if ($countsecond == 1) {
				$totalvalue = $total + $value + $total1;
			} else {
				// after adding the value
				$totalvalue = $total + $value;
			}
			
			if ($totalvalue < 100) {
				$update = $db->update ( $table, array ($fieldname => $value ), array ("id" => 1 ) );
				echo 1;
			} else {
				echo 2;
			}
		
		} else {
			$insert = $db->insert ( $table, array ($fieldname => $value ) );
			echo 1;
		}
	}
}
?>