Here is my updated code. What do you think?
<?php
/************************************************************\
* Config
\************************************************************/
$basedirectory = "http://localhost/websites/jc2/admin/";
$mainfile = "index.php";
/************************************************************\
* Connect to database
\************************************************************/
function dbconnect()
{
$host = ""; // Pretty sure you wont need to change that
$user = ""; // Your username
$pass = ""; // ... And password
$db = ""; // The database name
mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
}
/************************************************************\
* Disconnect from database
\************************************************************/
function dbdisconnect()
{
mysql_close();
}
/************************************************************\
* Show writing form
\************************************************************/
function showwriteform($title, $content)
{
echo "<br />";
echo "<form method=\"POST\" action=\"$mainfile\">";
echo "<div class=\"form\">";
echo "<label for=\"title\">Title:</label><input type=\"text\" name=\"title\" value=\"$title\" />";
echo "<br /><br />";
echo "<label for=\"content\">Content:</label>";
echo "<textarea name=\"content\" cols=\"60\" rows=\"20\" />$content</textarea>";
echo "<br /><br />";
echo "<input type=\"hidden\" name=\"action\" value=\"write\" />";
echo "<input type=\"submit\" name=\"submit\" value=\"Publish entry\">";
echo "</div>";
echo "</form>";
}/************************************************************\
* Show editing form
\************************************************************/
function showeditform($row)
{
echo "<br />";
echo "<form method=\"POST\" action=\"$mainfile\">";
echo "<div class=\"form\">";
echo "<label for=\"title\">Title:</label><input type=\"text\" name=\"title\" value=\"$row[1]\" />";
echo "<br /><br />";
echo "<label for=\"content\">Content:</label>";
echo "<textarea name=\"content\" cols=\"60\" rows=\"20\" />$row[2]</textarea>";
echo "<br /><br />";
echo "<input type=\"hidden\" name=\"action\" value=\"edit\" />";
echo "<input type=\"hidden\" name=\"id\" value=\"$row[0]\" />";
echo "<input type=\"submit\" name=\"submit\" value=\"Edit entry\">";
echo "</div>";
echo "</form>";
echo "<br />";
echo "<hr />";
}
/************************************************************\
* Protect from SQL injection
\************************************************************/
function quote_smart($value)
{
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
if (!is_numeric($value))
{
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
/************************************************************\
* Call to page
\************************************************************/
if($calltopage) {
dbconnect();
$query = "SELECT id, title, content, date FROM blog";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if (mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_row($result))
{
echo "<a name=\"$row[0]\" />";
echo "<h3>$row[1]</h3>";
echo "<p>$row[3]</p>";
echo "<p>$row[2]</p>";
echo "<hr />";
}
}
else
{
echo "<p class=\"notice\">There are no blog entries.</p>";
}
dbdisconnect();
}
else {
/************************************************************\
\************************************************************/
$showform = TRUE;
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
echo '<html xmlns="http://www.w3.org/1999/xhtml">';
echo '<head profile="http://gmpg.org/xfn/11">';
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
echo '<style type="text/css">';
echo "body{font-family:verdana,sans-serif;margin:0em 10em 0 10em}h1,h2,h3,h4{margin:0;font-weight:normal}.form{width:40em}.form label{width:6em;float:left}.form input,textarea{border:1px dotted navy;background-color:#fff}#container{width:50em}#header{text-align:center}#navigation{text-align:center}#navigation ul{list-style:none;margin:0;padding:0;border-bottom:1px solid #eee}#navigation li{display:inline;padding:1em}#navigation li a{text-decoration:none;color:blue;font-size:.8em}#content{font-size:1em}.error{color:red}.notice{color:green}#editlist a{text-decoration:none;color:blue;font-size:.8em}";
echo '</style>';
echo '<title></title>';
echo '</head>';
echo '<body>';
echo '<div id="container">';
echo '<div id="header">';
echo '<h2>Blog</h2>';
echo '</div>';
echo '<br />';
echo '<div id="navigation">';
echo '<ul>';
echo '<li><a href="' . $basedirectory . '">Home/Write entry</a></li>';
echo '<li><a href="' . $basedirectory . $mainfile . '?do=edit">Edit/Delete entries</a></li>';
echo '</ul>';
echo '</div>';
echo '<br />';
echo '<div id="content">';
if (isset($_POST['submit']))
{
$title = trim($_POST['title']);
$content = trim($_POST['content']);
$error = '';
$errors = '';
$date = date("jS\/F\/Y");
if($title == '')
{
$error[] = "Title";
}
if($content == '')
{
$error[] = "Content";
}
if($error)
{
foreach ($error as $error_list)
{
$errors .= "<li>" . $error_list . "</li>";
}
$message = '<p class="error">Please check your</p><ul>' . $errors . '</ul>';
$showform = TRUE;
}
else
{
if($action=="write")
{
dbconnect();
$query = sprintf("INSERT INTO blog (title, content, date) VALUES (%s, %s, '$date')",
quote_smart($_POST['title']),
quote_smart($_POST['content']));
$result = mysql_query($query);
$message = '<p class="notice">Posted.</p>';
dbdisconnect();
$showform = FALSE;
}
if($action=="edit")
{
dbconnect();
$query = sprintf("UPDATE blog SET title = %s, content = %s, date = '$date' WHERE id=$id",
quote_smart($_POST['title']),
quote_smart($_POST['content']));
$result = mysql_query($query);
dbdisconnect();
$message = '<p class="notice">Entry edited.</p>';
$showform = FALSE;
}
}
}
switch($act)
{
case "change":
dbconnect();
$query = "SELECT id, title, content, date FROM blog WHERE id=$id";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$row = mysql_fetch_row($result);
echo $message;
if($showform)
{
showeditform($row);
}
dbdisconnect();
break;
case "delete":
dbconnect();
$id = intval($_GET["id"]);
$query = "DELETE FROM blog WHERE id=$id";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
dbdisconnect();
break;
}
switch($do)
{
case "edit":
echo "<h3>Edit/Delete entries</h3>";
dbconnect();
$query = "SELECT id, title, content, date FROM blog";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if (mysql_num_rows($result) > 0)
{
echo "<ul id=\"editlist\">";
while($row = mysql_fetch_row($result))
{
echo "<li>";
echo $row[1];
echo " :: ";
echo "<a href=" . $mainfile . "?do=edit&act=change&id=" . $row[0] .">Edit</a>";
echo " | ";
echo "<a href=\"" . $mainfile . "?do=edit&act=delete&id=" . $row[0] ."\" onclick=\"this.innerHTML = 'Deleting...';\">Delete</a>";
}
echo "</ul>";
}
else
{
echo "<p class=\"error\">You have published no entries</p>";
}
dbdisconnect();
break;
default:
echo "<h3>Write entry</h3>";
echo $message;
if($showform)
{
showwriteform($title, $content);
}
}
echo '</div>';
echo '</div>';
echo '</body>';
echo '</html>';
}
?>
I have another page that calls the blog:
<!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 profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<body>
<?php $calltopage = TRUE;
include("admin/index.php");?>
</body>
</html>
How should I go about the date format? If I have it UNIX timestamped could I have php interpret that? That way I can change the interpreter, rather than having set date text in the column.
Also for logging in, I was thinking .htaccess the /admin directory. I could hardcode a password into the script maybe.