php essentials on lynda.com
Posted: Thu Aug 12, 2010 10:19 am
I'm working on the course php essential on lynda.com and on the last item in the crud section and having problems with new_page, page_edit, getting parse error and won't go to edit page.
Should be able to select sub menu ie. 'History' and then select "edit_page" link and go to that page which pulls up a page_form called in page_edit but I'm messed up.
Can someone please take a look and help. So far everything else works fine, just this last section.
Thanks,
Alex
Should be able to select sub menu ie. 'History' and then select "edit_page" link and go to that page which pulls up a page_form called in page_edit but I'm messed up.
Can someone please take a look and help. So far everything else works fine, just this last section.
Thanks,
Alex
Code: Select all
content.php
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page); ?>
<br />
<a href="new_subject.php">+ Add a new subject</a>
</td>
<td id="page">
<?php if (!is_null($sel_subject)) { // subject selected ?>
<h2><?php echo $sel_subject['menu_name']; ?></h2>
<?php } elseif (!is_null($sel_page)) { // page selected ?>
<h2><?php echo $sel_page['menu_name']; ?></h2>
<div class="page-content">
<?php echo $sel_page['content']; ?><br />
<br /><a href="edit_page.php?page=<?php echo $edit_page['id']; ?>">Edit page</a><br />
</div>
<?php } else { // nothing selected ?>
<h2>Select a subject or page to edit</h2>
<?php } ?>
</td>
</tr>
</table>
<?php require("includes/footer.php"); ?>
new_page.php
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
// make sure the subject id sent is an integer
if (intval($_GET['subj']) == 0) {
redirect_to("content.php");
}
include_once("includes/form_functions.php");
// START FORM PROCESSING
// only execute the form processing if the form has been submitted
if (isset($_POST['submit'])) {
// initialize an array to hold our errors
$errors = array();
//perform validations on the form data
$required_fields = array('menu_name', 'position', 'visible', 'content');
$errors = array_merge($errors, check_required_fields($required_fields));
$fields_with_lengths = array('menu_name' => 30);
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths));
// clean up the form data before putting it in the database
$id = mysql_prep($_GET['page']);
$menu_name = trim(mysql_prep($_POST['menu_name']));
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$content = mysql_prep($_POST['content']);
// Database submission only proceeds if there were NO errors.
if (empty($errors)) {
$query = "UPDATE pages SET
menu_name = '{$menu_name}',
position = {$position},
visible = {$visible},
content = '{$content}'
WHERE id = {$id}";
$result = mysql_query($query);
// test to see if the update occurred
if (mysql_affected_rows() == 1) {
// Success
$message = "The page was successfully updated.";
} else {
$message = "The page could not be updated.";
$message .= "<br />" . mysql_error();
}
} else {
if (count($errors) == 1) {
$message = "There was 1 error in the form.";
} else {
$message = "There were " . count($errors) . " errors in the form.";
}
}
// END FORM Processing
?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page, $public = false); ?>
<br />
<a href="new_subject.php">+ Add a new subject</a>
</td>
<td id="page">
<h2>Adding New Page</h2>
<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
<?php if (!empty($errors)) { display_errors($errors); } ?>
<form action="new_page.php?subj=<?php echo $sel_subject['id']; ?>" method="post">
<?php $new_page = true; ?>
<?php include "page_form.php" ?>
<input type="submit" name="submit" value="Create Page" />
</form>
<br />
<a href="edit_subject.php?subj=<?php echo $sel_subject['id']; ?>">Cancel</a><br />
</td>
</tr>
</table>
<?php include("includes/footer.php"); ?>
edit_page.php
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
// make sure the subject id sent is an integer
if (intval($_GET['page']) == 0) {
redirect_to('content.php');
}
include_once("includes/form_functions.php");
// START FORM PROCESSING
// only execute the form processing if the form has been submitted
if (isset($_POST['submit'])) {
// initialize an array to hold our errors
$errors = array();
//perform validations on the form data
$required_fields = array('menu_name', 'position', 'visible', 'content');
$errors = array_merge($errors, check_required_fields($required_fields));
$fields_with_lengths = array('menu_name' => 30);
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths));
// clean up the form data before putting it in the database
$id = mysql_prep($_GET['page']);
$menu_name = trim(mysql_prep($_POST['menu_name']));
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$content = mysql_prep($_POST['content']);
// Database submission only proceeds if there were NO errors.
if (empty($errors)) {
$query = "UPDATE pages SET
menu_name = '{$menu_name}',
position = {$position},
visible = {$visible},
content = '{$content}'
WHERE id = {$id}";
$result = mysql_query($query);
// test to see if the update occurred
if (mysql_affected_rows() == 1) {
// Success
$message = "The page was successfully updated.";
} else {
$message = "The page could not be updated.";
$message .= "<br />" . mysql_error();
}
} else {
if (count($errors) == 1) {
$message = "There was 1 error in the form.";
} else {
$message = "There were " . count($errors) . " errors in the form.";
}
}
// END FORM Processing
}
?>
<?php find_selected_page(); ?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page); ?>
<br />
<a href="new_subject.php">+ Add a new subject</a>
</td>
<td id="page">
<h2>Edit page: <?php echo $sel_page['menu_name']; ?></h2>
<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
<?php if (!empty($errors)) { display_errors($errors); } ?>
<form action="edit_page.php?page=<?php echo $sel_page['id']; ?>" method="post">
<?php include "page_form.php" ?>
<input type="submit" name="submit" value="Update Page" />
<a href="delete_page.php?page=<?php echo $sel_page['id']; ?>" onclick="return confirm('Are you sure you want to delete this page?');">Delete page</a>
</form>
<br />
<a href="content.php?page=<?php echo $sel_page['id']; ?>Cancel</a><br />
</td>
</tr>
</table>
<?php include("includes/footer.php"); ?>
page_form.php
<?php // this page is included by new_page.php and edit_page.php ?>
<?php if (!isset($new_page)) {$new_page = false;} ?>
<p>Page name: <input type="text" name="menu_name" value="<?php echo $sel_page['menu_name']; ?>" id="menu_name" /></p>
<p>Position: <select name="position">
<?php
if (!$new_page) {
$page_set = get_pages_for_subject($sel_page['subject_id']);
$page_count = mysql_num_rows($page_set);
} else {
$page_set = get_pages_for_subject($sel_subject['id']);
$page_count = mysql_num_rows($page_set) + 1;
}
for ($count=1; $count <= $page_count; $count++) {
echo "<option value=\"{$count}\"";
if ($sel_page['position'] == $count) { echo " selected"; }
echo ">{$count}</option>";
}
?>
</select></p>
<p>Visible:
<input type="radio" name="visible" value="0"<?php
if ($sel_page['visible'] == 0) { echo " checked"; }
?> /> No
<input type="radio" name="visible" value="1"<?php
if ($sel_page['visible'] == 1) { echo " checked"; }
?> /> Yes
</p>
<p>Content:<br />
<textarea name="content" rows="20" cols="80"><?php echo $sel_page['content']; ?></textarea>
</p>