<?php
$db=$GLOBALS ['db'];
if($_GET['id_cash']!='' && $_GET['name_user']!='' && $_GET['id_user']!=''){
	$c=$db->query("select id from ecmcashs_ecmkpkw where ecmcash_id='".$_GET['id_cash']."' and user_id='".$_GET['id_user']."'");
	if($c->num_rows==0){
		if($_GET['selected']==1){
			$c2=$db->query("select id from ecmcashs_ecmkpkw where user_id='".$_GET['id_user']."' and selected='".$_GET['selected']."'");
			if($c2->num_rows>0){
				while($wyn=$db->fetchByAssoc($c2)){
					$db->query("update ecmcashs_ecmkpkw set selected='0' where id='".$wyn['id']."'");
				}
			}
			
		} else {
			$_GET['selected']=0;
		}
		$db->query("insert into ecmcashs_ecmkpkw values ('".create_guid()."','".$_GET['id_cash']."','".$_GET['id_user']."','0','".$_GET['selected']."')");
		echo "insert into ecmcashs_ecmkpkw values ('".create_guid()."','".$_GET['id_cash']."','".$_GET['id_user']."','0','".$_GET['selected']."')";
	} else {
	echo "Kasa jest już powiązana z danym użytkownikiem";
	}
}