<?php

      $ipaddress = '';
      if (getenv('HTTP_CLIENT_IP'))
          $ipaddress = getenv('HTTP_CLIENT_IP');
      else if(getenv('HTTP_X_FORWARDED_FOR'))
          $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
      else if(getenv('HTTP_X_FORWARDED'))
          $ipaddress = getenv('HTTP_X_FORWARDED');
      else if(getenv('HTTP_FORWARDED_FOR'))
          $ipaddress = getenv('HTTP_FORWARDED_FOR');
      else if(getenv('HTTP_FORWARDED'))
          $ipaddress = getenv('HTTP_FORWARDED');
      else if(getenv('REMOTE_ADDR'))
          $ipaddress = getenv('REMOTE_ADDR');
      else
          $ipaddress = 'UNKNOWN';

      //echo $ipaddress;

	/* Grab the URL variable */
	$acode = $_GET["acode"];
	$eid = $_GET["eid"];
	$account = $_GET["account"];
	$broker = $_GET["broker"];

	$lastupdate = $_GET["lastupdate"];
	$version = $_GET["version"];
	$uindex = $_GET["uindex"];
	$eindex = $_GET["eindex"];
	$gindex = $_GET["gindex"];
	$cindex = $_GET["cindex"];
	$yindex = $_GET["yindex"];
	$pipgain = $_GET["pipgain"];
	$gjgain = $_GET["gjgain"];
	$ejgain = $_GET["ejgain"];
	$ujgain = $_GET["ujgain"];
	$eggain = $_GET["eggain"];
	$lasttrade = $_GET["lasttrade"];
	$lastlogindate = $_GET["lastlogindate"];
	$eacomment = $_GET["eacomment"];
	$magic = $_GET["magic"];
	
	
///////////////////new


	/* Connect to database and grab the keys */
		
	$servername = "localhost";
	$username = "u9cwxhx7pzvxt";
	$password = "Maggie@2014SG$2014%%";
	$dbname = "phibase0_FSP";

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


	$query = "SELECT `acode` FROM `acode`";

	$result = $conn->query($query);
	$num=$result->num_rows;	

	
	/* Set up a loop to check if requested key is found in database */
	$i=0;
	$validationkey = 0;

	$validation = "";


if ($result=mysqli_query($conn,$query))
{
	while ($obj=mysqli_fetch_object($result))
	{
		if ($acode == $obj->acode)
		{
			$validation = "Okay";
		}	
	}
	mysqli_free_result($result);
}	
	
	/* If a match was found in database, echo "VALID". Else echo "ERROR" */
	
	if($validation != "") {
	
		$result = mysqli_query($conn,"SELECT * FROM acode WHERE acode='{$acode}' ") 
		or die(mysql_error()); 
		
		while($row = mysqli_fetch_array($result))
		{
			$email = $row['email'];
		}
	}

$conn->close();

///////////////////end new	
	
	
	
	/* Connect to database and grab the keys */
		
$servername = "localhost";
$username = "u9cwxhx7pzvxt";
$password = "Maggie@2014SG$2014%%";
$dbname = "phibase0_FSP";

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

		$query = "SELECT `account` FROM `monitor`";
		$result = $conn->query($query);
		$num=$result->num_rows;	
		
echo $num;
			$i=0;
			$validationkey = 0;

			$validation = "";

				if ($result=mysqli_query($conn,$query))
				  {
				  while ($obj=mysqli_fetch_object($result))
					{
				 
					 if ($account == $obj->account)
					 {
						
						$validation = $account;
						echo  $validation , " || " , $obj->acode , " Found Account record";
						
						//echo $validation;
						$conn->query("UPDATE monitor SET email='$email' WHERE account='$account'") or 
										die("Could not UPDATE database");						
						$conn->query("UPDATE monitor SET broker='$broker' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET lastupdate='$lastupdate' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET eaversion='$version' WHERE account='$account'") or 
										die("Could not UPDATE database");							
						$conn->query("UPDATE monitor SET uindex='$uindex' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET eindex='$eindex' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET gindex='$gindex' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET cindex='$cindex' WHERE account='$account'") or 
										die("Could not UPDATE database");
						$conn->query("UPDATE monitor SET yindex='$yindex' WHERE account='$account'") or  
										die("Could not UPDATE database");
						$conn->query("UPDATE monitor SET pipgain='$pipgain' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET gjgain='$gjgain' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET ejgain='$ejgain' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET ujgain='$ujgain' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET eggain='$eggain' WHERE account='$account'") or 
										die("Could not UPDATE database");											
						$conn->query("UPDATE monitor SET lasttrade1='$lasttrade' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET ipaddress='$ipaddress' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET lastlogindate='$lastlogindate' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET eacomment='$eacomment' WHERE account='$account'") or 
										die("Could not UPDATE database");	
						$conn->query("UPDATE monitor SET magic='$magic' WHERE account='$account'") or 
										die("Could not UPDATE database");	
															
						
					 }
					}
				  mysqli_free_result($result);
				}															
	


		if ($validation == "")
		{
			
			if ($account != "")
			{
				$conn->query("INSERT INTO monitor (acode, email, account, broker,lastupdate, eaversion, uindex, eindex, gindex,cindex,yindex,pipgain,gjgain,
							ejgain,ujgain,lasttrade1,ipaddress, lastlogindate,eacomment,magic ) VALUES('$acode', '$email', '$account' , '$broker','$lastupdate', '$version', '$uindex', '$eindex', '$gindex', '$cindex', '$yindex', '$pipgain', '$gjgain', '$ejgain', '$ujgain', '$lasttrade','$ipaddress', '$lastlogindate', '$eacomment', '$magic')")
						or die(mysql_error()); 				
				echo "New Data Created ";
			}
		}
		else
		{
			echo "Existing Data Updated ";

		}	
	
	$conn->close();

?>