<?php

   	/* Grab the URL variable */
	$TID = $_GET["TID"];	
	$Magic = $_GET["Magic"];
	$SYM  = $_GET["SYM"];
	
	$Trade = $_GET["Trade"];
	$Open = $_GET["Open"];
	$TP = $_GET["TP"];
	$SL = $_GET["SL"];
	$Time = $_GET["Time"];
	
	//echo $TID . " " . $Magic .  " " . $SYM .  " " . $Open .  " " . $Trade  .   " " . $TP  .   " " . $SL  .  " " . $Time  . PHP_EOL;
	/* Connect to database and grab the keys */
		
$servername = "localhost";
$username = "u9cwxhx7pzvxt";
$password = "Maggie@2014SG$2014%%";
$dbname = "phibase0_ActSignals";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
////////////////////////////

		$query = "SELECT `TID` FROM `squant`";
		$result = $conn->query($query);
		$num=$result->num_rows;	
		

			$i=0;
			$validationkey = 0;

			$validation = "";

				if ($result=mysqli_query($conn,$query))
				  {
				  while ($obj=mysqli_fetch_object($result))
					{
				 
					 if ($TID == $obj->TID)
					 {
						
						$validation = $TID;
						echo  $validation , " || " , $obj->SYM , " Found Account record";
						
						//echo $validation;
						$conn->query("UPDATE squant SET Magic='$Magic' WHERE TID='$TID'") or 
										die("Could not UPDATE database");						
						$conn->query("UPDATE squant SET SYM='$SYM' WHERE TID='$TID'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE squant SET Trade='$Trade' WHERE TID='$TID'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE squant SET Open='$Open' WHERE TID='$TID'") or 
										die("Could not UPDATE database");							
						$conn->query("UPDATE squant SET TP='$TP' WHERE TID='$TID'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE squant SET SL='$SL' WHERE TID='$TID'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE squant SET LastTimeStamp='$Time' WHERE TID='$TID'") or 
										die("Could not UPDATE database");	
							
					 }
					}
				  mysqli_free_result($result);
				}															
	

		if ($validation == "")
		{
			
			if ($TID != "")
			{
				$conn->query("INSERT INTO squant (TID, Magic, SYM, Trade, Open, TP, SL, LastTimeStamp) VALUES('$TID', '$Magic', '$SYM' , '$Trade','$Open', '$TP', '$SL', '$Time')")
						or die(mysql_error()); 				
				echo "New Data Created ";
			}
		}
		else
		{
			echo "Existing Data Updated " . $TID . " " . $Magic .  " " . $SYM .  " " . $Open .  " " . $Trade  .   " " . $TP  .   " " . $SL  .  " " . $Time  . PHP_EOL;;

		}	
	
	$conn->close();

?>