<?php
require_once ("varables.php");
$myJSON = 0;
if(isset($_POST["nekot"]) && isset($_POST["json"]))
{
	$postdata = $_POST["json"];
	$categoris = json_decode($postdata,true);
$sort=0;
	$toke = $_POST["nekot"];
if($toke == "sldiskdjgsoewmvmdpekdjghsncusi"){
    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
        @mysqli_query($connect,"SET CHARACTER SET utf8;");

					for($i=0;$i<sizeof($categoris);$i++){
						$select_query = "SELECT id FROM ".$tbl_category1." WHERE s_local_id ='".$categoris[$i]["S_GroupCode"]."' AND m_local_id='".$categoris[$i]["M_GroupCode"]."'";
							$res =@mysqli_query($connect,$select_query);
							if(!$res){
								$myJSON = @mysqli_error($connect);
									echo $myJSON;
								return;
							}
							else {
								$count=0;
								while($row = @mysqli_fetch_assoc($res)){
									$count++;
								}
								if($count == 0){
									$select_cat1 = "SELECT id FROM ".$tbl_category." WHERE local_id = '".$categoris[$i]["M_GroupCode"]."'";
								$selectres =@mysqli_query($connect,$select_cat1);
								if($select_cat1){
									$cont=0;
									$id = 0;
									while($row1 = @mysqli_fetch_assoc($selectres)){
										$cont++;
										$id = $row1["id"];
									}
								if($cont>0){
										$insert_query= "INSERT INTO ".$tbl_category1." (cat1name,cat,sort,m_local_id,s_local_id)
											VALUES ('".$categoris[$i]["S_GroupName"]."','".$id."','".$sort."','".$categoris[$i]["M_GroupCode"]."','".$categoris[$i]["S_GroupCode"]."')";
											$insres =@mysqli_query($connect,$insert_query);
											if(!$insres){
											$myJSON = @mysqli_error($connect);
											  echo  $myJSON;
											return;
										}
									}
									}
								}
								else{
									$update_query= "UPDATE ".$tbl_category1." SET cat1name = '".$categoris[$i]["S_GroupName"]."' WHERE s_local_id ='".$categoris[$i]["S_GroupCode"]."' AND m_local_id='".$categoris[$i]["M_GroupCode"]."'";
										$upres =@mysqli_query($connect,$update_query);
										if(!$upres){
										$myJSON = @mysqli_error($connect);
											echo $myJSON;
										return;
								}
							}
				}
			}
				$myJSON = 200;
		}
	}
	 else
		 $myJSON =405;
}
    echo $myJSON;
?>
