<?php
$DB_SERVER = "localhost";
// $DB_USER = "root";
// $DB_PASS = "";
// $DB_NAME = "test";
$DB_USER = "collectr_blogger";
$DB_PASS = "blogpass";

// $con = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS);


mysql_select_db($DB_NAME);

$getNoOfQuotesQuery = mysql_query("select count(*) from quotes", $con);
$no_of_quotes = mysql_result($getNoOfQuotesQuery, 0);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Quote Editor :: Quotes:<?=$no_of_quotes?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12pt;
	color: #333333;
}
body {
	background-color: #E0DFE3;
}
a {
	font-weight: bold;
}
-->
</style></head>
<body>

<?php

if ($submit) {
    echo "<h1>Submit:</h1>";

    $quote = mysql_escape_string($quote);
    $author = mysql_escape_string($author);
    $result = mysql_query("UPDATE `quotes` SET `quote` = '" . $quote . "',`author` = '" . $author . "'  WHERE `id` = '" . $id . "'", $con);

    if ($result == 1) {
        echo "<h1>Change made!";
        echo "<br><br><a href=\"quote.php\">Go Back</a></h1>";
    } 
} elseif ($insert) {
    echo "<h1>Insert:</h1>";

    $quote = mysql_escape_string($quote);
    $author = mysql_escape_string($author);

    $result = mysql_query("INSERT INTO `quotes` (`quote` , `author` ) VALUES ('" . $quote . "', '" . $author . "')", $con);

    echo "INSERT INTO `quotes` (`quote` , `author` ) VALUES ('" . $quote . "', '" . $author . "')";

    if ($result == 1) {
        echo "<h1>Insert made!";
        echo "<br><br><a href=\"quote.php\">Go Back</a></h1>";
    } else {
        echo "Error on insert!";
    } 

    echo $result;
} elseif ($delete) {
    echo "<h1>Delete:</h1>";

    if ($id) {
        $result = mysql_query("DELETE FROM quotes WHERE id = '" . $id . "'", $con);
    } 

    if ($result == 1) {
        echo "<h1>Delete made!";
        echo "<br><br><a href=\"quote.php\">Go Back</a></h1>";
    } else {
        echo "Error on delete!";
    } 

    echo $result;
} elseif ($id) {
    $result = mysql_query("SELECT * FROM quotes where id=" . $id . " ORDER BY id", $con);

    echo "<table border=1 align=left>";

    while ($myrow = mysql_fetch_array($result)) {
        echo "<tr>";

        ?>
<form action="quote.php?submit=1" method="post" name="form" id="form">
  <p>Quote: <textarea name="quote" cols="80" id="quote"><?= $myrow["quote"]?>
</textarea>
  </p>
  <p>Author: <input name="author" type="text" id="author" value="<?= $myrow["author"]?>" />
  </p>
  <p>
    <input name="id" type="hidden" value="<?= $myrow["id"]?>" />
     <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
<?php

        echo "</td></tr>";
        echo "</table>";
    } 
    loop;
} else {

    ?>
<h1>New:</h1>
<form action="quote.php?insert=1" method="post" name="form" id="form">
  <p>Quote: <textarea name="quote" cols="80" id="quote"></textarea>
  </p>
  <p>Author: <input name="author" type="text" id="author" />
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
<?php
    echo "<h1>Quotes:</h1>";
    $result = mysql_query("SELECT * FROM quotes ORDER BY id DESC", $con);

    if ($myrow = mysql_fetch_array($result)) {

        ?>
<form name="form" method="post" action="quote.php">
<input name="id" type="hidden" value="<?= $myrow["id"]?>" />
<?php
        echo "<table border=1 align=left>";

        do {
            echo "<tr>";

            printf("<td><a href=\"quote.php?id=%s\" onClick=\"submit()\">%s</td>", $myrow["id"], $myrow["id"]);
            printf("<td><span align=left>%s</span>-%s\n", $myrow["quote"], $myrow["author"]);
            printf("<td><a href=\"quote.php?delete=1&id=%s\" onclick=\"javascript:return confirm('Are you sure you want delete?')\">Delete</td>", $myrow["id"], $myrow["id"]);

            echo "</td></tr>";
        } while ($myrow = mysql_fetch_array($result));

        echo "</table>";
    } 
    echo "</form>";
} 

mysql_close($con);

?>
</p>
</body>
</html>
