<?php

// *******Extremely important with php*********
// You must observe 'case' when using all $vars.   $sql is not the same as $Sql
// You will not get an error, it just won't work
//
$Command=htmlspecialchars($_GET["Cmd"]);
$Id=htmlspecialchars($_GET["Id"]);
$IdFrom=htmlspecialchars($_GET["IdF"]);
$IdTo=htmlspecialchars($_GET["IdT"]);
$Type=$Data=htmlspecialchars($_GET["Ty"]);
$Data=htmlspecialchars($_GET["Dt"]);

//replace single quote to prevent sql injection
$Commnand=str_replace("'"," ",$Command);
$Id=str_replace("'"," ",$Id);
$IdFrom=str_replace("'"," ",$IdFrom);
$IdTo=str_replace("'"," ",$IdTo);
$Type=str_replace("'"," ",$Type);
$Data=str_replace("'"," ",$Data);
//replace double quote to prevent sql injection-note use of single quote to specify the replaced char-tricky
$Commnand=str_replace('"'," ",$Command);
$Id=str_replace('"'," ",$Id);
$IdFrom=str_replace('"'," ",$IdFrom);
$IdTo=str_replace('"'," ",$IdTo);
$Type=str_replace('"'," ",$Type);
$Data=str_replace('"'," ",$Data);

echo "Cmd1: ".$Command." ".$Id." ".$IdFrom." ".$IdTo." ".$Type." ".$Data."\r\n";

// Open the DataBase-must be modified to your values
  $host_name = 'dbwwwwww.hosting-data.io';
  $database = 'dbsxxxxx';
  $user_name = 'dbuyyyyy';
  $password = 'zzzzzz';
  
  $link = new mysqli($host_name, $user_name, $password, $database);
  if ($link->connect_error) {
    die("Fail1:Failed to connect to MySQL: ". $link->connect_error ."\r\n");
  } else {
    echo "Conn1:Connection to MySQL \r\n";
  }

switch ($Command) {
  case "BT":
      //Remove // comments if you need to drop the table and start over
      //$sql = "Drop TABLE tblSync";
      //if ($link->query($sql) === TRUE) {
      //  echo "Tbl1: Table tblSync dropped \r\n";
      //} else {
      //  echo "Err1: Error dropping table: " . $conn->error."\r\n";
      //}
    
      $sql = "CREATE TABLE if not exists tblSync (
      Id text, IdFrom text, IdTo text, sType text, sFlag text, sData text )";
	  echo $sql."\r\n";

      if ($link->query($sql) === TRUE) {
        echo "Tbl1: Table tblSync created \r\n";
      } else {
        echo "Err1: Error creating table: " . $conn->error."\r\n";
      }
	  Break;
    
  case "IR":
      $sql = "INSERT INTO tblSync ( Id , IdFrom , IdTo , sType, sFlag, sData )
      VALUES ('".$Id."','".$IdFrom."','".$IdTo."','".$Type."','0','".$Data."')";
      echo $sql."\r\n";
      
     if ($link->query($sql) === TRUE) {
        echo "Add1: New record added \r\n";
      } else {
        echo "Err1: Error: " . $sql . "<br>" . $conn->error. "\r\n";
      }
	  break;
  
     case "SF":
       $sql = "Update tblSync set sFlag='1' where Id='".$Id. "' and IdTo='".$IdTo."' " ; 
       echo $sql."\r\n";
     
     if ($link->query($sql) === TRUE) {
        echo "Set1: Flag set to 1 \r\n";
      } else {
        echo "Err1: Error: " . $sql . "<br>" . $conn->error. "\r\n";
      }
      // now, get the record count
      $sql="SELECT count(*) from tblSync where Id='".$Id. "' and IdTo='".$IdTo."' and sFlag='1'";
      $count = $link->query($sql)->fetch_row()[0];
      echo "Flag1 Records Flagged:<Ct>" . $count. "<ECt>\r\n";
      break;
  
    case "DR":
       $sql = "Delete from tblSync where Id='".$Id. "' and IdTo='".$IdTo."' and sFlag='1' " ; 
       echo $sql."\r\n";
     
     if ($link->query($sql) === TRUE) {
        echo "Del1: Records Deleted \r\n";
      } else {
        echo "Err1: Error: " . $sql . "<br>" . $conn->error. "\r\n";
      }
	  break;
 
  case "RR":  
     $sql = "SELECT * FROM tblSync where Id='".$Id. "' and IdTo='".$IdTo."' and sFlag='1' order by IdFrom, sType" ; 
     echo $sql."\r\n";
     //Run the Query 
 	 $result = $link->query($sql);
     //If the query returned results, loop through each result 
     if ($result->num_rows > 0) {
              while($row = $result->fetch_assoc()) {
              echo "Rec1: <Ty>" .  $row['sType']."<IdF>".$row['IdFrom']."<Dt> ". $row['sData']. "<EDt>\r\n"; 
         }
         $result -> free_result();
         } else {
           echo "0 results \r\n";
       }
       break;
    
//End Case
}

   // closing connection
   $link->close();
   echo "Close1:DB Closed";
  

?>