<?php 

include('../classes/database.class.php');

$db = new database();

$gettoddaydate = date('Y-m-d');

$sym = $_POST['sym'];

$getMaxValue = "SELECT MAX(rate) FROM currencyrate WHERE date = '$gettoddaydate' AND cur = '$sym'";
$getMax = $db->db->prepare($getMaxValue);
$getMax->execute();
$getMaxValuee = $getMax->fetch();
$max_value = $getMaxValuee[0];

$getMinQuery = "SELECT MIN(rate) FROM currencyrate WHERE date = '$gettoddaydate' AND cur = '$sym'";
$getMin = $db->db->prepare($getMinQuery);
$getMin->execute();
$getMinValue = $getMin->fetch();
$min_value = $getMinValue[0];

echo $max_value."<br>";
echo $min_value."<br>";

$getTr = $max_value - $min_value;

echo $getTr."<br>";

$gettrquery = $db->db->prepare("SELECT * FROM atr WHERE date = '$gettoddaydate' AND sym = '$sym'");
$gettrquery->execute();
$gettrvalue = $gettrquery->fetch();

if (empty($gettrvalue)) {
	# code...
	$insertatr = $db->db->prepare("INSERT INTO atr(tr, sym, date) VALUES ('$getTr', '$sym', '$gettoddaydate')");
	$insertatr->execute();

}else{

	$updateatr = $db->db->prepare("UPDATE atr SET tr = '$getTr' WHERE sym = '$sym' and date = '$gettoddaydate'");
	if ($updateatr->execute()) {
		# code...
		echo "Updated";
	}

}


?>