Hey All,
Having a bit of a problem figuring out how-to get a form to work. Little bit of context: There are eBooks / titles and there are pages that belong to these eBooks. Just pages of plain text. So, I have a “books” table that contains bookID (primary key), bookTitle, and a few other fields like book description and some foreign keys for the author, publisher, etc. I have another table called “pages” that contains pageID (primary key), pageText, pageSlug and bookID (foreign key).
I want to use a form on a web page to add a page of text to a particular book. So, there’s a drop-down box where you can select the title / eBook you want to add the page of text to, a text field for entering the pageSlug (which is actually the real page number) and a textarea for adding the actual page content to pageText.
Firstly, the drop-down box isn’t retrieving the existing bookTitles from the books table and it basically continues to go down hill from there The code I have, I’ve taken from Kevin Yank’s PHP & MySQL: Novice to Ninja book, and edited it slightly for my particular needs, so that may be why it isn’t working…But if I could ask you guys for a little bit of help on why it’s not working and how-to get it working then that’d be great!
Here’s the code I’ve got!
pages.html
<?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="Description unavailable.">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Pages</title>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" media="screen" href="../../css/stylesheet.css">
</head>
<body>
<article>
<header>
<h1>Add Pages</h1>
</header>
<div id="content">
<form action="?" method="post">
<div>
<label for="book">eBook Title:</label>
<select name="book" id="book">
<option value="">Select Title</option>
<?php foreach ($books as $book): ?>
<option value="<?php htmlout($book['id']); ?>">
<?php htmlout($book['title']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="pageslug">Page Number:</label>
<input type="text" id="pageslug" name="pageslug"/>
</div>
<div>
<label for="pagetext">Page Text:</label>
<textarea id="pagetext" name="pagetext" rows="10" cols="40"><?php htmlout($content); ?></textarea>
</div>
<div>
<input type="hidden" name="id" value="<?php htmlout($id); ?>" />
<input type="submit" value="Add Page">
</div>
</form>
</div>
</article>
</body>
</html>
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';
if (isset($_GET['add']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbconnection.inc.php';
// Build the list of books
try
{
$result = $pdo->query('SELECT bookID, bookTitle FROM books');
}
catch (PDOException $e)
{
$error = 'Error fetching list of books.';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$books[] = array('bookID' => $row['id'], 'bookTitle' => $row['title']);
}
include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbconnection.inc.php';
if ($_POST['book'] == '')
{
$error = 'You must choose a book for this page.
Click ‘back’ and try again.';
include 'error.html.php';
exit();
}
try
{
$sql = 'INSERT INTO pages SET
pagetext = :pagetext,
pageslug = :pageslug,
bookID = :bookID';
$s = $pdo->prepare($sql);
$s->bindValue(':pagetext', $_POST['text']);
$s->bindValue(':pageslug', $_POST['slug']);
$s->bindValue(':bookID', $_POST['book']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error adding new page.';
include 'error.html.php';
exit();
}
$pageID = $pdo->lastInsertId();
header('Location: .');
exit();
}
include 'pages.html.php';
And the error.html.php is just the same as pages.html, with the exception that all of the PHP code is replaced by
echo $error
.
This is what the page looks like curently:
Any ideas or pointers? Greatly appreciated, as always.