<?php
//tag listánál ez adja vissza a keresés eredményét
ini_set('include_path', '../_class/');
include('class_sql.php');
if ($_SERVER['HTTP_HOST'] == 'badmintoncoach.hu') $sql = new sql('bc_mysql','root','','badminton_coach');
else $sql = new sql('localhost','tollashodos','uprRscU8bGpJ','tollashodos');


//ha benne van, akkor törölni kell, egyébként insertelni

$ugk_query = "SELECT ugk_id FROM user_group_kid WHERE ugk_user_group_ug_id = " . $_POST['group_id']. " AND ugk_user_kid_uk_id = " . $_POST['uk_id'] . ";";

if ($sql->num_of_rows($ugk_query)) {
	//ha nincs category-ja, akkor töröljük, egyébként csak nullra rakjuk a group_id-t
	$ugk_id = $sql->single_variable($ugk_query);

	$check_query = "SELECT ugk_category_ugc_id FROM user_group_kid WHERE ugk_user_group_ug_id = " . $_POST['group_id']. " AND ugk_user_kid_uk_id = " . $_POST['uk_id'] . ";";
	$category_id = $sql->single_variable($check_query);
	if ($category_id) {
		//update to null
		$sql->update_table('user_group_kid', array('ugk_user_group_ug_id' => 'null'), array('ugk_id' => $ugk_id));
	}
	else {
		$sql->execute_query("DELETE FROM user_group_kid WHERE ugk_id = " . $ugk_id);
	}


}
else {
	//ha még nincs benne null-lal, akkor insert, egyébként update
	$check_query = "SELECT ugk_id FROM user_group_kid WHERE ugk_user_group_ug_id IS NULL AND ugk_user_kid_uk_id = " . $_POST['uk_id'] . ";";
	$ugk_id = $sql->single_variable($check_query);
	if ($ugk_id) {
		//update
		$sql->update_table('user_group_kid', array(
				'ugk_user_group_ug_id' => $_POST['group_id']
			), array('ugk_id' => $ugk_id));
	}
	else {
		$sql->insert_into('user_group_kid', array(
				'ugk_user_kid_uk_id' => $_POST['uk_id'],
				'ugk_user_group_ug_id' => $_POST['group_id']
			));
	}

}

?>