Jump to content
Larry Ullman's Book Forums

Can't Get Add_Page To Display Pg. 116-132

Recommended Posts



I cannot get add_page.php to display. I am on pages 116 - 132. I used the author's file (rather than create my own) and followed along in the book. I can't figure out what the problem is so I have been stuck on this part for 6 hours. Any advise please? I am on a deadline (yikes).


Thanks so much!


Link to comment
Share on other sites

Hi daviddawn, 


Thanks for you quick reply and advice. Here is the code for add_page.php. I was able to follow the tutorial in the book until this point. Then, when I uploaded this file, the form would show up and I cannot see what is wrong. I uploaded it as is from the author's source files:


// This page is used by an administrator to create a specific page of HTML content.
// This script is created in Chapter 5.
// Require the configuration before any PHP code as the configuration controls error reporting:
// If the user isn't logged in as an administrator, redirect them:
// Require the database connection:
// Include the header file:
$page_title = 'Add a Site Content Page';
// For storing errors:
$add_page_errors = array();
// Check for a title:
if (empty($_POST['title'])) {
$t = escape_data(strip_tags($_POST['title']), $dbc);
} else {
$add_page_errors['title'] = 'Please enter the title!';
// Check for a category:
if (filter_var($_POST['category'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
$cat = $_POST['category'];
} else { // No category selected.
$add_page_errors['category'] = 'Please select a category!';
// Check for a description:
if (!empty($_POST['description'])) {
$d = escape_data(strip_tags($_POST['description']), $dbc);
} else {
$add_page_errors['description'] = 'Please enter the description!';
// Check for the content:
if (!empty($_POST['content'])) {
$allowed = '<div><p><span><br><a><img><h1><h2><h3><h4><ul><ol><li><blockquote>';
$c = escape_data(strip_tags($_POST['content'], $allowed), $dbc);
} else {
$add_page_errors['content'] = 'Please enter the content!';
if (empty($add_page_errors)) { // If everything's OK.
// Add the page to the database:
$q = "INSERT INTO pages (categories_id, title, description, content) VALUES ($cat, '$t', '$d', '$c')";
$r = mysqli_query($dbc, $q);
if (mysqli_affected_rows($dbc) === 1) { // If it ran OK.
// Print a message:
echo '<div class="alert alert-success"><h3>The page has been added!</h3></div>';
// Clear $_POST:
$_POST = array();
// Send an email to the administrator to let them know new content was added?
} else { // If it did not run OK.
trigger_error('The page could not be added due to a system error. We apologize for any inconvenience.');
} // End of $add_page_errors IF.
} // End of the main form submission conditional.
// Need the form functions script, which defines create_form_input():
<h1>Add a Site Content Page</h1>
<form action="add_page.php" method="post" accept-charset="utf-8">
<fieldset><legend>Fill out the form to add a page of content:</legend>
create_form_input('title', 'text', 'Title', $add_page_errors); 
// Add the category drop down menu:
echo '<div class="form-group';
if (array_key_exists('category', $add_page_errors)) echo ' has-error'; 
echo '"><label for="category" class="control-label">Category</label>
<select name="category" class="form-control">
<option>Select One</option>';
// Retrieve all the categories and add to the pull-down menu:
$q = "SELECT id, category FROM categories ORDER BY category ASC";
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
echo "<option value=\"$row[0]\"";
// Check for stickyness:
if (isset($_POST['category']) && ($_POST['category'] == $row[0]) ) echo ' selected="selected"';
echo ">$row[1]</option>\n";
echo '</select>';
if (array_key_exists('category', $add_page_errors)) echo '<span class="help-block">' . $add_page_errors['category'] . '</span>';
echo '</div>';
create_form_input('description', 'textarea', 'Description', $add_page_errors); 
create_form_input('content', 'textarea', 'Content', $add_page_errors); 
<input type="submit" name="submit_button" value="Add This Page" id="submit_button" class="btn btn-default" />
// Include the footer file to complete the template:
Thanks in advance for your help!
Link to comment
Share on other sites

Sorry, the missing bracket is in the script (I just missed it when I copied the code and pasted here). However, I did find the error log whic says:


PHP Fatal error:  Cannot redeclare create_form_input()  previously declared in ---webroot path---/includes/form_functions.inc.php: 13 in ---webroot path---includes/form_functions.inc.php on line 87

Link to comment
Share on other sites

Thanks so much for your help! The errors seem to be coming from the form_functions.inc.php file. I have made changes on line 87, even deleting it entirely (it's actually just a comment) and I still get the same error.


I also changed the add_pages file to require_once for including the form_functions.inc.php file.


Here is a copy of the form_functions,inc.php file unchanged from the source file which is how I had uploaded it to the server originally:


I have highlighted lines 13 and 87 in red


// This script defines any functions required by the various forms.
// This script is created in Chapter 3.
// This function generates a form INPUT or TEXTAREA tag.
// It takes five arguments:
// - The name to be given to the element.
// - The type of element (text, password, textarea).
// - The label for the element
// - An array of errors.
// - An array of additional options.
function create_form_input($name, $type, $label = '', $errors = array(), $options = array()) {
// Assume no value already exists:
$value = false;
// Check for a value in POST:
if (isset($_POST[$name])) $value = $_POST[$name];
// Strip slashes if Magic Quotes is enabled:
if ($value && get_magic_quotes_gpc()) $value = stripslashes($value);
// Start the DIV:
echo '<div class="form-group';
// Add a class if an error exists:
if (array_key_exists($name, $errors)) echo ' has-error';
// Complete the DIV:
echo '">';
// Create the LABEL, if one was provided:
if (!empty($label)) echo '<label for="' . $name . '" class="control-label">' . $label . '</label>';
// Conditional to determine what kind of element to create:
if ( ($type === 'text') || ($type === 'password') || ($type === 'email')) {
// Start creating the input:
echo '<input type="' . $type . '" name="' . $name . '" id="' . $name . '" class="form-control"';
// Add the value to the input:
if ($value) echo ' value="' . htmlspecialchars($value) . '"';
// Check for additional options:
if (!empty($options) && is_array($options)) {
foreach ($options as $k => $v) {
echo " $k=\"$v\"";
// Complete the element:
echo '>';
// Show the error message, if one exists:
if (array_key_exists($name, $errors)) echo '<span class="help-block">' . $errors[$name] . '</span>';
} elseif ($type === 'textarea') { // Create a TEXTAREA.
// Show the error message above the textarea (if one exists):
if (array_key_exists($name, $errors)) echo '<span class="help-block">' . $errors[$name] . '</span>';
// Start creating the textarea:
echo '<textarea name="' . $name . '" id="' . $name . '" class="form-control"';
// Check for additional options:
if (!empty($options) && is_array($options)) {
foreach ($options as $k => $v) {
echo " $k=\"$v\"";
// Complete the opening tag:
echo '>';
// Add the value to the textarea:
if ($value) echo $value;
// Complete the textarea:
echo '</textarea>';
} // End of primary IF-ELSE.
// Complete the DIV:
echo '</div>';
} // End of the create_form_input() function.
// Omit the closing PHP tag to avoid 'headers already sent' errors!
Link to comment
Share on other sites

Hi daviddawn, 


I just wanted to thank you again for all your help. I re-downloaded the files from the site, made the require_once change on the add_pages.php file and when I uploaded it to the server, I checked the "overwrite previous files" and IT WORKED!!



Link to comment
Share on other sites

Glad its working for you buddy, however, line 87  "} // End of the create_form_input() function." is not all one comment, the curly brace is still interpreted by php but the text that follows the forward slash's "//" is read as a comment.



Link to comment
Share on other sites


  • Create New...