This is where I am at as of now.
<?php
include_once $_SERVER['DOCUMENT_ROOT'] .
'/includes/magic.inc.php';
if (isset($_GET['add']))
{
$pageTitle = 'New Article';
$action = 'addform';
$text = '';
$authorid = '';
$contentid = '';
$button = 'Add Article';
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
// Build the list of authors
try
{
$result = $pdo->query('SELECT id, name FROM author');
}
catch (PDOException $e)
{
$error = 'Error fetching list of authors.';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$authors[] = array('id' => $row['id'], 'name' => $row['name']);
}
// Build the list of categories
try
{
$result = $pdo->query('SELECT id, name FROM category');
}
catch (PDOException $e)
{
$error = 'Error fetching list of categories.';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$categories[] = array(
'id' => $row['id'],
'name' => $row['name'],
'selected' => FALSE);
}
include 'form.html.php';
exit();
}
if (isset($_GET['addform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
if ($_POST['author'] == '')
{
$error = 'You must choose an author for this article.
Click ‘back’ and try again.';
include 'error.html.php';
exit();
}
try
{
$sql = 'INSERT INTO content SET
headline = :headline,
content = :content,
cdate = CURDATE(),
authorid = :authorid';
$s = $pdo->prepare($sql);
$s->bindParam (':headline', $_POST['headline']);
$s->bindParam (':content', $_POST['content']);
$s->bindParam (':authorid', $_POST['author']);
$s->execute();
}
catch (PDOException $e)
{
//$error = 'Error adding submitted article.';
$error = 'Failed to Add article to mySQL DB' . $e->getMessage();
include 'error.html.php';
exit();
}
$contentid = $pdo->lastInsertId();
if (isset($_POST['categories']))
{
try
{
$sql = 'INSERT INTO Ccategory SET
contentid = :contentid,
categoryid = :categoryid';
$s = $pdo->prepare($sql);
foreach ($_POST['categories'] as $categoryid)
{
$s->bindValue(':contentid', $contentid);
$s->bindValue(':categoryid', $categoryid);
$s->execute();
}
}
catch (PDOException $e)
{
$error = 'Failed to Add Categories to mySQL DB' . $e->getMessage();
include 'error.html.php';
exit();
}
}
header('Location: .');
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'Edit')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$sql = 'SELECT id, headline, content, authorid FROM content WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error fetching article details.';
include 'error.html.php';
exit();
}
$row = $s->fetch();
$pageTitle = 'Edit article';
$action = 'editform';
$headline = $row['headline'];
$content = $row['content'];
$authorid = $row['authorid'];
$id = $row['id'];
$button = 'Update Article';
// Build the list of authors
try
{
$result = $pdo->query('SELECT id, name FROM author');
}
catch (PDOException $e)
{
$error = 'Error fetching list of authors.';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$authors[] = array('id' => $row['id'], 'name' => $row['name']);
}
// Get list of categories containing this article
try
{
$sql = 'SELECT categoryid FROM Ccategory WHERE contentid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $id);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error fetching list of selected categories.';
include 'error.html.php';
exit();
}
foreach ($s as $row)
{
$selectedCategories[] = $row['categoryid'];
}
// Build the list of all categories
try
{
$result = $pdo->query('SELECT id, name FROM category');
}
catch (PDOException $e)
{
$error = 'Error fetching list of categories.';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$categories[] = array(
'id' => $row['id'],
'name' => $row['name'],
'selected' => in_array($row['id'], (array)$selectedCategories));
}
include 'form.html.php';
exit();
}
if (isset($_GET['editform']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
if ($_POST['author'] == '')
{
$error = 'You must choose an author for this article
Click ‘back’ and try again.';
include 'error.html.php';
exit();
}
try
{
$sql = 'UPDATE content SET
headline = :headline,
content = :content,
authorid = :authorid
WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindParam(':id', $_POST['id']);
$s->bindParam(':headline', $_POST['headline']);
$s->bindParam(':content', $_POST['content']);
$s->bindParam(':authorid', $_POST['author']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error updating submitted Article.'. $e->getMessage();;
include 'error.html.php';
exit();
}
try
{
$sql = 'DELETE FROM Ccategory WHERE contentid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error removing obsolete joke category entries.';
include 'error.html.php';
exit();
}
if (isset($_POST['categories']))
{
try
{
$sql = 'INSERT INTO Ccategory SET
contentid = :contentid,
categoryid = :categoryid';
$s = $pdo->prepare($sql);
foreach ($_POST['categories'] as $categoryid)
{
$s->bindValue(':contentid', $_POST['id']);
$s->bindValue(':categoryid', $categoryid);
$s->execute();
}
}
catch (PDOException $e)
{
$error = 'Error inserting article into selected categories.';
include 'error.html.php';
exit();
}
}
header('Location: .');
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
// Delete category assignments for this article
try
{
$sql = 'DELETE FROM Ccategory WHERE contentid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error removing article from categories.';
include 'error.html.php';
exit();
}
// Delete the article
try
{
$sql = 'DELETE FROM content WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error deleting article.';
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
if (isset($_GET['action']) and $_GET['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
// The basic SELECT statement
$select = 'SELECT id, headline, content';
$from = ' FROM content';
$where = ' WHERE TRUE';
$placeholders = array();
if ($_GET['author'] != '') // An author is selected
{
$where .= " AND authorid = :authorid";
$placeholders[':authorid'] = $_GET['author'];
}
if ($_GET['category'] != '') // A category is selected
{
$from .= ' INNER JOIN Ccategory ON id = contentid';
$where .= " AND categoryid = :categoryid";
$placeholders[':categoryid'] = $_GET['category'];
}
if ($_GET['text'] != '') // Some search text was specified
{
$where .= " AND headline, content LIKE :content";
$placeholders[':content'] = '%' . $_GET['text'] . '%';
}
try
{
$sql = $select . $from . $where;
$s = $pdo->prepare($sql);
$s->execute($placeholders);
}
catch (PDOException $e)
{
$error = 'Error fetching articles.';
include 'error.html.php';
exit();
}
foreach ($s as $row)
{
$articles[] = array('id' => $row['id'], 'headline'=> $row['headline'], 'text' => $row['content']);
}
include 'content.html.php';
exit();
}
// Display search form
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$result = $pdo->query('SELECT id, name FROM author');
}
catch (PDOException $e)
{
$error = 'Error fetching authors from database!';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$authors[] = array('id' => $row['id'], 'name' => $row['name']);
}
try
{
$result = $pdo->query('SELECT id, name FROM category');
}
catch (PDOException $e)
{
$error = 'Error fetching categories from database!';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$categories[] = array('id' => $row['id'], 'name' => $row['name']);
}
include 'searchform.html.php';
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/includes/helpers.inc.php'; ?>
<!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>Welcome To MarX ProShop <?php htmlout($pageTitle); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<link rel="stylesheet" type="text/css" href="http://www.marxproshop.com/css.css" />
</head>
<body bgcolor="a22727">
<?php include($_SERVER['DOCUMENT_ROOT'] . "/banner.php"); ?>
<div id="Content">
<h1><?php htmlout($pageTitle); ?></h1>
<form action="?<?php htmlout($action); ?>" method="post">
<div>
<label for="headline">Type your headline here:</label></div>
<div>
<textarea id="headline" name="headline" rows="" cols="40">
<?php htmlout($headline); ?></textarea>
</div>
<div>
<label for="content">Type your Article here:</label>
</div>
<div>
<textarea id="content" name="content" rows="5" cols="40"><?php
htmlout($content); ?></textarea>
</div>
<div>
<label for="author">Author:</label>
<select name="author" id="author">
<option value="">Select one</option>
<?php foreach ($authors as $author): ?>
<option value="<?php htmlout($author['id']); ?>"<?php
if ($author['id'] == $authorid)
{
echo ' selected';
}
?>><?php htmlout($author['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<fieldset>
<legend>Categories:</legend>
<?php foreach ($categories as $category): ?>
<div>
<label for="category<?php htmlout($category['id']); ?>">
<input type="checkbox" name="categories[]" id="category<?php htmlout($category['id']); ?> "value="<?php htmlout($category['id']); ?> "<?php
if ($category['selected'])
{
echo ' checked';
}
?>> <?php htmlout($category['name']); ?> </label> </div>
<?php endforeach; ?>
</fieldset>
<div>
<input type="hidden" name="id" value="<?php
htmlout($contentid); ?>">
<input type="submit" value="<?php htmlout($button); ?>">
</div>
</form>
<?php include($_SERVER['DOCUMENT_ROOT'] . "/footer.php"); ?>
</div>
</body>
</html>