<?php
require_once ("varables.php");

$myJSON = 0;
if(isset($_POST["nekot"]) && isset($_POST["json"]))
{

	$postdata = $_POST["json"];
	$visitors = json_decode($postdata,true);

	$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($visitors);$i++){
						$select_query = "SELECT id FROM ".$tbl_visitors." WHERE V_id ='".$visitors[$i]["V_id"]."'";
							$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){
									if($visitors[$i]["V_tell"].str_replace(" ","",$visitors[$i]["V_tell"]) != ""){
										$insert_query= "INSERT INTO ".$tbl_visitors." (V_name,V_id,V_Code,V_tell,username,pass)
											VALUES ('".$visitors[$i]["V_Name"]."','".$visitors[$i]["V_id"]."','".$visitors[$i]["V_Code"]."','".$visitors[$i]["V_tell"]."','".$visitors[$i]["V_tell"]."','123456')";
											$insres =@mysqli_query($connect,$insert_query);
											if(!$insres){
												$myJSON = @mysqli_error($connect);
											  echo  $myJSON;
											return;
										}
									}
								}
								else{
									$update_query= "UPDATE ".$tbl_visitors." SET V_name = '".$visitors[$i]["V_Name"]."', V_Code = '".$visitors[$i]["V_Code"]."', V_tell = '".$visitors[$i]["V_tell"]."' WHERE V_id ='".$visitors[$i]["V_id"]."'";
										$upres =@mysqli_query($connect,$update_query);
										if(!$upres){
											$myJSON = @mysqli_error($connect);
											echo $myJSON;
										return;
								}
							}
				}
			}
				$myJSON = 200;
		}
	}
	 else
		 $myJSON ="خطای اعتبار سنجی...!";
}
    echo $myJSON;

?>
