<?php
	session_start();
	include("ini.php");
	$db = $_SESSION["Db"];
	$queryName = "";
	$querySql = "";
	if(isset($_GET["QueryName"]))
	{
		$queryName = $_GET["QueryName"];
		$_SESSION["QueryName"]=$queryName;
	}
	else
		$queryName = $_SESSION["QueryName"];
	
	if(isset($_GET["DelSql"]))
	{
		$queryName = $_GET["DelSql"];
		$dbcon = mysqli_connect (SERVER, USER, PASSWORD, $db);
		if (mysqli_connect_errno()==0)
		{
			$sql = "DELETE FROM x_sql WHERE QueryName='".$queryName."'";
			mysqli_query ($dbcon, $sql);
			$queryName = "";
		}
		mysqli_close($dbcon);
	}

	if(isset($_GET["CreateSql"]))
	{
		$queryName = $_GET["CreateSql"];
		$dbcon = mysqli_connect (SERVER, USER, PASSWORD, $db);
		if (mysqli_connect_errno()==0 && $db != "")
		{
			$sql = "INSERT INTO x_sql (QueryName,QuerySql) VALUES ('".$queryName."','SELECT ...')";
			mysqli_query ($dbcon, $sql);
			$_SESSION["QueryName"]=$queryName;
		}
		mysqli_close($dbcon);
	}

	if(isset($_GET["Save"]) && $queryName!= "")
	{
		$querySql = $_POST["QuerySql"];
		$dbcon = mysqli_connect (SERVER, USER, PASSWORD, $db);
		if (mysqli_connect_errno()==0)
		{
			$sql = "UPDATE x_sql SET QuerySql='".$querySql."' WHERE QueryName='".$queryName."'";
			mysqli_query ($dbcon, $sql);
		}
		mysqli_close($dbcon);
	}

	if($queryName!= "")
	{
		$dbcon = mysqli_connect (SERVER, USER, PASSWORD, $db);
		if (mysqli_connect_errno()==0)
		{
			$sql = "SELECT QuerySql FROM x_sql WHERE QueryName='".$queryName."'";
			$querySqls = mysqli_query ($dbcon, $sql);
			$qquerySql = mysqli_fetch_array($querySqls);
			$querySql = $qquerySql[0];
		}
		mysqli_close($dbcon);
	}
	
?>
<!DOCTYPE html>
<html lang="de">
	<head>
		<link rel="stylesheet" href="Standard.css">
		<title>Execute SQL-Statements</title>
	</head>
	<body>
		<header>
			<h1>Execute SQL-Statements on <font color='red'><?php echo $db;?></font></h1>
		</header>		
			<nav>
				<form action="ExecuteSql.php"><input type="submit" value="Neues SQL-Statement erstellen: "><input type="text" name="CreateSql" size="30"></form>
			</nav>

		<aside>
			<h2>Stored Statements</h2>			 
<?PHP 
		$dbcon = mysqli_connect (SERVER, USER, PASSWORD, $db);
		if (mysqli_connect_errno()==0)
		{
			$sql = "SELECT * FROM x_sql";
			$queryNames = mysqli_query ($dbcon, $sql);
			while($qqueryName = mysqli_fetch_array($queryNames))
			{
				echo "<a href='ExecuteSql.php?DelSql=".$qqueryName[0]."' method='POST'><img src='Delete.png'></a>&nbsp;";
				if($qqueryName[0] == $queryName) 
					echo "<a href='ExecuteSql.php?QueryName=".$qqueryName[0]."'><font color = 'red'>".$qqueryName[0] ."</font></a><br>";
				else
					echo "<a href='ExecuteSql.php?QueryName=".$qqueryName[0]."'>".$qqueryName[0] ."</a><br>";
			}
		}
		mysqli_close($dbcon);
?>		</aside>
		<article>
			<h2><?PHP echo $queryName; ?></h2>
				<form action="ExecuteSql.php?Save=true" method="POST">
					<textarea name="QuerySql" cols="80" rows="5"><?PHP echo $querySql;?></textarea>
					<input type="submit" value="Save">
				</form>
				<form action="ExecuteSql.php?Exe=true" method="POST"><input type="submit" value="Execute">				</form>			
			<section>
<?PHP 
	if(isset($_GET["Exe"]) && $querySql!= "")
	{
		$dbcon = mysqli_connect (SERVER, USER, PASSWORD, $db);
		if (mysqli_connect_errno()==0)
		{
			if(stripos($querySql,"SELECT")===0)
			{
				echo "<table>";
				$rows = mysqli_query ($dbcon, $querySql);
				$runOnce=true;
				while($row = mysqli_fetch_assoc($rows))
				{
					if($runOnce)
					{
						$runOnce=false;
						echo "<tr>";
						foreach($row as $key => $value)
							echo "<th>".$key."</th>";
						echo "</tr>";
						
					}
					echo "<tr>";
					$nrCols = count($row);
					foreach($row as $key => $value)
						echo "<td>".$value."</td>";
					echo "</tr>";
				}
				echo "</table>";	
			}
			else
			{
				if(mysqli_multi_query ($dbcon, $querySql))
					echo "done";
				else
					echo "Error executing Sql-Statement.";
			}
		}
		mysqli_close($dbcon);
	}
?>	
			</section>
		</article>
		<aside>
			<h2>Aktionen</h2>
			<p>
				<a href="Index.php">Zur&uuml;ck zur Hauptseite</a>
			</p>
		</aside>
		<footer>
			<p>&copy; 2019 by L&uuml;der Reimers</p>
		</footer>  
	</body>
</html>