October 20, 200917 yr I'm following this tutorial for a CMS and the page is meant to update the navigation bar links. In their version on the video it updates but on mine it doesn't. I'll post the code of the two pages it used and if you can see why it doesn't update that would be a great help. edit_subject.php <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php find_selected_page();?> <?php if (intval($_GET['subj']) == 0) { redirect_to("content.php"); } if (isset($_POST['submit'])) { $errors = array(); $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) { $errors[] = $fieldname; } } $fields_with_lengths = array('menu_name' => 30); foreach($fields_with_lengths as $fieldname => $maxlength ) { if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; } } if (empty($errors)) { // Perform Update $id = mysql_prep($_GET['subj']); $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']); $query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = '{$visible} WHERE id = {$id}"; $result = mysql_query($query, $connection); if (mysql_affected_rows() ==1) { // Success } else { // Failed } } else { //Errors occured } } // end: if (isset($_POST['submit'])) ?> <?php include("includes/header.php"); ?> <table id="structure"> <tr> <td id="navigation"> <?php echo navigation($sel_subject, $sel_page); ?> </td> <td id="page"> <h2>Edit Subject: <?php echo $sel_subject['menu_name']; ?></h2> <form action="edit_subject.php?subj=<?php echo urlencode($sel_subject['id']); ?>" method="post"> <p>Subject name: <input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>" id="menu_name" /> </p> <p>postion: <select name="position"> <?php $subject_set = get_all_subjects(); $subject_count = mysql_num_rows($subject_set); //subject_count + 1 becayse we are adding a subject for($count=1; $count <= $subject_count; $count++) { echo "<option value=\"{$count}\""; if ($sel_subject['position'] == $count) { echo " selected"; } echo ">{$count}</option>"; } ?> </select> </p> <p>Visible: <input type="radio" name="visible" value="0"<?php if ($sel_subject['visible'] == 0) { echo " checked"; } ?> /> No <input type="radio" name="visbile" value="1"<?php if ($sel_subject['visible'] ==1) { echo " checked"; } ?> /> Yes </p> <input type="submit" name="submit" value="Edit subject" /> </form> <br /> <a href="content.php">Cancel</a> </td> </tr> </table> <?php require("includes/footer.php"); ?> functions.php <?php // This file is the place to store all basic functions function mysql_prep( $value ) { $magic_quotes_active = get_magic_quotes_gpc(); $new_enough_php = function_exists( "mysql_real_escape_string" ); //i.e. PHP >= v4. 3.0 if( $new_enough_php ) { //PHP v4.3.0 or higher // undo any magic quote effects so mysql_real_escape_string can do the work if( $magic_quotes_active ) { $value = stripsplases( $value ); } $value = mysql_real_escape_string( $value ); } else { // before PHP v4.3.0 // if maic quotes aren't already on then add slashes manually if ( !magic_quotes_active ) { $value = addslashes( $value ); } // if magic quotes are active, then the slashes already exist } return $value; } function redirect_to( $location = NULL ) { if($location !=NULL) { header("Location: {$location}"); exit; } } function confirm_query($result_set) { if (!$result_set) { die("Database query failed: " . mysql_error()); } } function get_all_subjects() { global $connection; $query = "SELECT * FROM subjects ORDER BY position ASC"; $subject_set = mysql_query($query, $connection); confirm_query($subject_set); return $subject_set; } function get_pages_for_subject($subject_id) { global $connection; $query = "SELECT * FROM pages WHERE subject_id = {$subject_id} ORDER BY position ASC"; $page_set = mysql_query($query, $connection); confirm_query($page_set); return $page_set; } function get_subject_by_id($subject_id) { global $connection; $query = "SELECT * "; $query .= "FROM subjects "; $query .= "WHERE id=" . $subject_id ." "; $query .= "LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); // REMEMBER: // if no rows are returned, fetch_array will return false if ($subject = mysql_fetch_array($result_set)) { return $subject; } else { return NULL; } } function get_page_by_id($page_id) { global $connection; $query = "SELECT * "; $query .= "FROM pages "; $query .= "WHERE id=" . $page_id ." "; $query .= "LIMIT 1"; $result_set = mysql_query($query, $connection); confirm_query($result_set); // REMEMBER: // if no rows are returned, fetch_array will return false if ($page = mysql_fetch_array($result_set)) { return $page; } else { return NULL; } } function find_selected_page() { global $sel_subject; global $sel_page; if (isset($_GET['subj'])) { $sel_subject = get_subject_by_id($_GET['subj']); $sel_page = NULL; } elseif (isset($_GET['page'])) { $sel_subject = NULL; $sel_page = get_page_by_id($_GET['page']); } else { $sel_subject = NULL; $sel_page = NULL; } } function navigation($sel_subject, $sel_page) { $output = "<ul class=\"subjects\">"; $subject_set = get_all_subjects(); while ($subject = mysql_fetch_array($subject_set)) { $output .= "<li"; if ($subject["id"] == $sel_subject['id']) { $output .= " class=\"selected\""; } $output .= "><a href=\"edit_subject.php?subj=" . urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a></li>"; $page_set = get_pages_for_subject($subject["id"]); $output .= "<ul class=\"pages\">"; while ($page = mysql_fetch_array($page_set)) { $output .= "<li"; if ($page["id"] == $sel_page['id']) { $output .= " class=\"selected\""; } $output .= "><a href=\"content.php?page=" . urlencode($page["id"]) . "\">{$page["menu_name"]}</a></li>"; } $output .= "</ul>"; } $output .= "</ul>"; return $output; } ?>
Create an account or sign in to comment