<?php
require_once('config.php');
//require_once('f_update_budget.php');

$error_message;
$valid = true;


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}


$tkn_site = $_REQUEST['tkn_site'];
$tkn_dvc_id = $_REQUEST['tkn_dvc_id'];
$tkn_user = $_REQUEST['tkn_user'];
$tkn_token = $_REQUEST['tkn_token'];

$sql= "UPDATE		tkn_mst 
		SET			tkn_token = '".$tkn_token."'					
		WHERE		tkn_site = '".$tkn_site."'	
		AND			tkn_user = '".$tkn_user."'	
		AND			tkn_dvc_id = '".$tkn_dvc_id."'	
		AND			tkn_online = '1'";

$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
	$error_message = "Error insert table (UPDATE Table tkn_mst)";
	returnError($error_message);
	die( print_r( sqlsrv_errors(), true));
}
sqlsrv_free_stmt( $stmt);

 if($stmt){

	 sqlsrv_commit( $conn );
	 returnData();

 }else{
	sqlsrv_rollback( $conn );
	$error_message = "Transaction rolled back.<br />";
	returnError($error_message);
 }
 
 
function returnData(){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Token updated successfully');
	
	echo json_encode($returnData);
}

function returnError($error_message){
	$json = array();
	
	$returnData = array(
	'status' => 'ERROR',
	'message' => $error_message,
	'data' => $json);
	
	echo json_encode($returnData);
	
		
	exit();
}
 
?>