<?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_category." WHERE local_id ='".$categoris[$i]["M_GroupCode"]."'";
							$res =@mysqli_query($connect,$select_query);
							if(!$res){
								$myJSON = 401;
									echo $myJSON;
								return;
							}
							else {
								$count=0;
								while($row = @mysqli_fetch_assoc($res)){
									$count++;
								}
								if($count == 0){
										$insert_query= "INSERT INTO ".$tbl_category." (catname,sort,local_id)
											VALUES ('".$categoris[$i]["M_GroupName"]."','".$sort."','".$categoris[$i]["M_GroupCode"]."')";
											$insres =@mysqli_query($connect,$insert_query);
											if(!$insres){
											$myJSON = 402;
											  echo  $myJSON;
											return;
										}

								}
								else{
									$update_query= "UPDATE ".$tbl_category." SET catname = '".$categoris[$i]["M_GroupName"]."' WHERE local_id ='".$categoris[$i]["M_GroupCode"]."'";
										$upres =@mysqli_query($connect,$update_query);
										if(!$upres){
										$myJSON = 402;
											echo $myJSON;
										return;
								}
							}
				}
			}
				$myJSON = 200;
		}
	}
	 else
		 $myJSON =405;
}
    echo $myJSON;
?>
