<?php session_start();

	if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  
  	include("configi.php");


  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conn, $theValue) : mysqli_escape_string($conn, $theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
     case "text22":
      $theValue = ($theValue != "") ? $theValue : "NULL";
      break; 
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

//echo "dave22";

	include("configi.php");

//	echo "job fields" . stripslashes($_POST['jobfields']);
	
	// Test if our data came through
if (isset($_POST["jobfields"])) {

//echo "data came through";
    // Decode our JSON into PHP objects we can use

//    $jobfields = json_decode($_POST['jobfields']);
	   $jobfields = json_decode(stripslashes($_POST['jobfields']));
	   
    // Access our object's data and array values.
    //echo "sales_id is: " . $jobfields->sales_id . "<br>";
   // echo "field_name is: " . $jobfields->field_name . "<br>";
   // echo "field_type is: " . $jobfields->field_type . "<br>";
   // echo "field_name1_db is: " . $jobfields->field_name1_db . "<br>";
   // echo "field_name2_db is: " . $jobfields->field_name2_db . "<br>";
   // echo "field_value1 is: " . $jobfields->field_value1 . "<br>";
   // echo "field_value2 is: " . $jobfields->field_value2 . "<br>";
		
}else{
	echo "data did not come through";
}

	
//echo $jobfields;

$outlet_id = 1;

$cat_id		=	$jobfields->cat_id;
$field_name		=	$jobfields->field_name;
$field_type		=	$jobfields->field_type;
$field_name1_db	=	$jobfields->field_name1_db;
$field_name2_db	=	$jobfields->field_name2_db;	
$field_value1	=	$jobfields->field_value1;
$field_value2	=	$jobfields->field_value2;
$old_status		=	$jobfields->old_status;
$outlet_id		=	$jobfields->outlet_id;
//$phxcrmclient_id	=	'94';

//echo "field name is " . $field_name;
//$field_name1_db = 'c_acctaddr2';
//echo "dave".$field_type."client".$cat_id;
//$cat_id = 100;


if ($field_type == "inputcheck"){
			
		$field_value1	=	GetSQLValueString($field_value1, "long");

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_web_cat set ".$field_name1_db."=".$field_value1." where cat_id = ".$cat_id;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		echo $result_message;
		
		exit;

}else if ($field_type == "input"){
			
		$field_value1	=	GetSQLValueString($field_value1, "text");

	//	echo $field_value1;

		$queryupdate = "update ".$_COOKIE['phxcrmclient_id']."_web_cat set ".$field_name1_db."=".$field_value1." where cat_id = ".$cat_id;
	
	//	echo $queryupdate;

		$result=mysqli_query($conn, $queryupdate);

		if($result){ 
			$result_message = "Update Successful"; 
		} else { 
			$result_message = "update failed" . $result . mysqli_error($conn);
		}
		
		echo $result_message;
		
		exit;
		

		}
		


