<?php
// pg.php
// PostGreSQL query
// RJM Programming
// May, 2015
// Thanks to: http://stackoverflow.com/questions/10640821/how-do-i-enable-php-to-work-with-postgresql
//            http://php.net/manual/en/function.pg-query.php
// 

echo "<!doctype html><html><head><title>PostGreSQL Form - RJM Programming - May, 2015</title><style> td { border:2px red solid; } </style></head><body onload=\"document.getElementById('code').focus(); \" style='background-color:yellow;'><h1 align='center'>PostGreSQL Form - RJM Programming - May, 2015</h1><br>";
if (!isset($_GET['oops'])) {
 try {
 $conn = pg_pconnect("host=localhost port=5432 dbname=databasename user=username password=password");
 if (!$conn) {
  echo "An Error occurred.\n";
  exit;
 }
 if (isset($_GET['code']) && ($_GET['code'] != "") && isset($_GET['delete'])) {
   $sql = "DELETE from films WHERE code='" . $_GET['code'] . "'";
   $result = pg_query($conn, $sql);
 } else if (isset($_GET['code']) && ($_GET['code'] != "") && strpos($_GET['did'],"-") === false && isset($_GET['title']) && isset($_GET['did']) && isset($_GET['date_prod']) && isset($_GET['kind']) && isset($_GET['len']) && ($_GET['title'] != "") && ($_GET['did'] != "") && ($_GET['date_prod'] != "") && ($_GET['kind'] != "") && ($_GET['len'] != "")) {
   $result = pg_query($conn, "INSERT INTO films (code,title,did,date_prod,kind,len) values ('" . $_GET['code'] . "','" . $_GET['title'] . "'," . $_GET['did'] . ",to_date('" . $_GET['date_prod'] . "','YYYY-MM-DD'),'" . $_GET['kind'] . "','" . $_GET['len'] . "')");
 } else if (isset($_GET['code']) && ($_GET['code'] != "")) {
   $delim=" ";
   $sql = "UPDATE films SET WHERE code='" . $_GET['code'] . "'";
   if (isset($_GET['title']) && ($_GET['title'] != "")) {
     $sql = str_replace(" WHERE ", $delim . " title='" . $_GET['title'] . "'" . " WHERE ", $sql);
     $delim = ", ";
   }
   if (isset($_GET['did']) && strpos($_GET['did'],"-") === false && ($_GET['did'] != "")) {
     $sql = str_replace(" WHERE ", $delim . " did=" . $_GET['did'] . " WHERE ", $sql);
     $delim = ", ";
   }
   if (isset($_GET['date_prod']) && ($_GET['date_prod'] != "")) { 
     $sql = str_replace(" WHERE ", $delim . " date_prod=to_date('" . $_GET['date_prod'] . "','YYYY-MM-DD')" . " WHERE ", $sql);
     $delim = ", ";
   }
   if (isset($_GET['kind']) && ($_GET['kind'] != "")) {
     $sql = str_replace(" WHERE ", $delim . " kind='" . $_GET['kind'] . "'" . " WHERE ", $sql);
     $delim = ", ";
   }
   if (isset($_GET['len']) && ($_GET['len'] != "")) {
     $sql = str_replace(" WHERE ", $delim . " len='" . $_GET['len'] . "'" . " WHERE ", $sql);
     $delim = ", ";
   }
   $result = pg_query($conn, $sql);
 }
 $result = pg_query($conn, "SELECT * FROM films");
 if (!$result) {
     echo "An error Occurred.\n";
     exit;
 }

 echo "<div align='center' style='background-color: pink;'><table cellspacing=5 cellpadding=5><tr><th>Film Code</th><th>Title</th><th>Id</th><th>Date</th><th>Kind</th><th>Length</th></tr>";
 while ($row = pg_fetch_row($result)) {
     echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td></tr>";
 } 
 echo "</table></div>"; 
 pg_close($conn);
 } catch(PDOException $e) {
      echo $e->getMessage();
 }
} else {           
 try {
   $dbh = new PDO('pgsql:host=localhost;port=5432;dbname=databasename;user=username;password=password');
 if (isset($_GET['code']) && ($_GET['code'] != "") && isset($_GET['delete'])) {
   $sql = "DELETE from films WHERE code='" . $_GET['code'] . "'";
   $result = $dbh->query($sql);
 } else if (isset($_GET['code']) && ($_GET['code'] != "") && strpos($_GET['did'],"-") === false && isset($_GET['title']) && isset($_GET['did']) && isset($_GET['date_prod']) && isset($_GET['kind']) && isset($_GET['len']) && ($_GET['title'] != "") && ($_GET['did'] != "") && ($_GET['date_prod'] != "") && ($_GET['kind'] != "") && ($_GET['len'] != "")) {
   $result = $dbh->query("INSERT INTO films (code,title,did,date_prod,kind,len) values ('" . $_GET['code'] . "','" . $_GET['title'] . "'," . $_GET['did'] . ",to_date('" . $_GET['date_prod'] . "','YYYY-MM-DD'),'" . $_GET['kind'] . "','" . $_GET['len'] . "')");
 } else if (isset($_GET['code']) && ($_GET['code'] != "")) {
   $delim=" ";
   $sql = "UPDATE films SET WHERE code='" . $_GET['code'] . "'";
   if (isset($_GET['title']) && ($_GET['title'] != "")) { 
     $sql = str_replace(" WHERE ", $delim . " title='" . $_GET['title'] . "'" . " WHERE ", $sql);
     $delim = ", ";
   }
   if (isset($_GET['did']) && strpos($_GET['did'],"-") === false && ($_GET['did'] != "")) {
     $sql = str_replace(" WHERE ", $delim . " did=" . $_GET['did'] . " WHERE ", $sql);
     $delim = ", ";
   }
   if (isset($_GET['date_prod']) && ($_GET['date_prod'] != "")) { 
     $sql = str_replace(" WHERE ", $delim . " date_prod=to_date('" . $_GET['date_prod'] . "','YYYY-MM-DD')" . " WHERE ", $sql);
     $delim = ", ";
   }
   if (isset($_GET['kind']) && ($_GET['kind'] != "")) {
     $sql = str_replace(" WHERE ", $delim . " kind='" . $_GET['kind'] . "'" . " WHERE ", $sql);
     $delim = ", ";
   }
   if (isset($_GET['len']) && ($_GET['len'] != "")) {
     $sql = str_replace(" WHERE ", $delim . " len='" . $_GET['len'] . "'" . " WHERE ", $sql);
     $delim = ", ";
   }
   $result = $dbh->query($sql);
 }
   echo "<div align='center' style='background-color: pink;'><table cellspacing=5 cellpadding=5><tr><th>Film Code</th><th>Title</th><th>Id</th><th>Date</th><th>Kind</th><th>Length</th></tr>";
   foreach ($dbh->query("SELECT * FROM films") as $row) {
     echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td></tr>";
   } 
   echo "</table></div>"; 
   $dbh->close();
   }
 catch(PDOException $e)
 {
      echo $e->getMessage();
 }
}

echo "<br><br><div align='center' style='background-color: lightgray;'><form method='GET' action='./pg.php'>";
if (isset($_GET['oops'])) echo " <input type='hidden' name='oops' value='" . $_GET['oops'] . "'></input> <table cellspacing=5 cellpadding=5>";
echo "<table cellspacing=5 cellpadding=5><tr><th>Film Code: </th><th><input id='code' style='width:220px;' type='text' name='code' value=''></input></th></tr>";
echo "<tr><th>    Title: </th><th><input style='width:220px;' type='text' name='title' value=''></input></th></tr>";
echo "<tr><th>       Id: </th><th><input style='width:220px;' type='number' name='did' value='-1'></input></th></tr>";
echo "<tr><th>     Date: </th><th><input style='width:220px;' type='text' name='date_prod' value='2000-12-31'></input></th></tr>";
echo "<tr><th>     Kind: </th><th><input style='width:220px;' type='text' name='kind' value=''></input></th></tr>";
echo "<tr><th>   Length: </th><th><input style='width:220px;' type='text' name='len' value='01:00:00'></input></th></tr>";
echo "</table><br><input type='submit' value='Update or Insert'></input>&nbsp;<input name='delete' type='submit' value='Delete'></input><br><p>&nbsp;</p></div>";


echo "</body></html>";

?>