j05hr Posted October 20, 2009 Share Posted October 20, 2009 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2009 Share Posted October 20, 2009 It would be really nice if you told us what exactly it does do when you try it. A blank page (and if so, what does the "view source" in your browser of the blank page show)? A php error? An error output from your code, such as "Database query failed ..."? Does it just redisplay the form? Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-940782 Share on other sites More sharing options...
j05hr Posted October 21, 2009 Author Share Posted October 21, 2009 Sorry, I was meant to explain but got to into posting the code and forgot. It redisplays the form with what the subject originally said. The view source after submitted <html> <head> <title>Widget Corp</title> <link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1>Widget Corp</h1> </div> <div id="main"><table id="structure"> <tr> <td id="navigation"> <ul class="subjects"><li><a href="edit_subject.php?subj=1">About Widget Corp</a></li><ul class="pages"><li><a href="content.php?page=1">History</a></li><li><a href="content.php?page=2">Our Mission</a></li></ul><li class="selected"><a href="edit_subject.php?subj=4">whatever</a></li><ul class="pages"></ul><li><a href="edit_subject.php?subj=2">Products</a></li><ul class="pages"><li><a href="content.php?page=3">Widget 2000</a></li></ul><li><a href="edit_subject.php?subj=3">Services</a></li><ul class="pages"></ul></ul> </td> <td id="page"> <h2>Edit Subject: whatever</h2> <form action="edit_subject.php?subj=4" method="post"> <p>Subject name: <input type="text" name="menu_name" value="whatever" id="menu_name" /> </p> <p>postion: <select name="position"> <option value="1" selected>1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option> </select> </p> <p>Visible: <input type="radio" name="visible" value="0" /> No <input type="radio" name="visbile" value="1" checked /> Yes </p> <input type="submit" name="submit" value="Edit subject" /> </form> <br /> <a href="content.php">Cancel</a> </td> </tr> </table> <br /> <b>Parse error</b>: parse error in <b>C:\wamp\www\widget_corp\includes\footer.php</b> on line <b>9</b><br /> Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-940962 Share on other sites More sharing options...
Gayner Posted October 21, 2009 Share Posted October 21, 2009 Sorry, I was meant to explain but got to into posting the code and forgot. It redisplays the form with what the subject originally said. The view source after submitted <html> <head> <title>Widget Corp</title> <link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1>Widget Corp</h1> </div> <div id="main"><table id="structure"> <tr> <td id="navigation"> <ul class="subjects"><li><a href="edit_subject.php?subj=1">About Widget Corp</a></li><ul class="pages"><li><a href="content.php?page=1">History</a></li><li><a href="content.php?page=2">Our Mission</a></li></ul><li class="selected"><a href="edit_subject.php?subj=4">whatever</a></li><ul class="pages"></ul><li><a href="edit_subject.php?subj=2">Products</a></li><ul class="pages"><li><a href="content.php?page=3">Widget 2000</a></li></ul><li><a href="edit_subject.php?subj=3">Services</a></li><ul class="pages"></ul></ul> </td> <td id="page"> <h2>Edit Subject: whatever</h2> <form action="edit_subject.php?subj=4" method="post"> <p>Subject name: <input type="text" name="menu_name" value="whatever" id="menu_name" /> </p> <p>postion: <select name="position"> <option value="1" selected>1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option> </select> </p> <p>Visible: <input type="radio" name="visible" value="0" /> No <input type="radio" name="visbile" value="1" checked /> Yes </p> <input type="submit" name="submit" value="Edit subject" /> </form> <br /> <a href="content.php">Cancel</a> </td> </tr> </table> <br /> <b>Parse error</b>: parse error in <b>C:\wamp\www\widget_corp\includes\footer.php</b> on line <b>9</b><br /> Instead of us scrambling to try to find what line bla bla, just give us the code and the specific line for us to work on, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-940976 Share on other sites More sharing options...
j05hr Posted October 21, 2009 Author Share Posted October 21, 2009 That's the thing though, there is no errors, it's just sending back what was already written and I have no idea what it could be but thought the more advanced coders might but obviously not. The code is in the OP. Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-940979 Share on other sites More sharing options...
PFMaBiSmAd Posted October 21, 2009 Share Posted October 21, 2009 Stating what it is doing helps pin down what path the code takes. Testing with the posted form and the posted code shows that it is likely that the mysql_query() is being executed. The following lines of code are missing some important code to report errors and notify you what the code did do - if (mysql_affected_rows() ==1) { // Success } else { // Failed } } else { //Errors occurred } Replace the above with this - if (mysql_affected_rows() ==1) { // Success echo "One row was updated<br />"; } else { // Failed die("Update query failed, query: $query<br />Rows: " . mysql_affected_rows() . "<br />Error: " . mysql_error()); } } else { //Errors occured echo "<pre>",print_r($errors,true),"</pre>"; } Also, add the following two lines of code immediately after your first opening <?php tag (move the require_once(...) statement down as necessary) - ini_set("display_errors", "1"); error_reporting(E_ALL); And you are getting a php parse error having something to do with footer.php. Either the require() statement actually contains a URL that you edited for the post and footer.php contains a syntax error or footer.php is in turn including/requiring a file using a URL that contains a syntax error. If any of your require/include statements are actually URL's that you edited for the post, please only xxxxx out any sensitive information in them that you don't want to show, but don't change the actual syntax being used in anything. Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-941058 Share on other sites More sharing options...
j05hr Posted October 21, 2009 Author Share Posted October 21, 2009 Not sure if this helps you bit this is what it came up with. Array ( [0] => visible ) Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-941062 Share on other sites More sharing options...
PFMaBiSmAd Posted October 21, 2009 Share Posted October 21, 2009 I edited my post above with some additional information that you should do, though it has no direct bearing on your current problem. That value in the $errors array means that neither radio button was selected. Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-941065 Share on other sites More sharing options...
j05hr Posted October 21, 2009 Author Share Posted October 21, 2009 I added the... ini_set("display_errors", "1"); error_reporting(E_ALL); After the line... <?php require_once("includes/connection.php"); ?> Is that right? I'm not sure what you mean about the parse error in the footer as I haven't changed any of the code for the post and the footer is in a sub folder called includes so I'm not sure how to fix that as the <?php require("includes/footer.php"); ?> is copied letter for letter from the tutorial? The buttons are selected though when I go to edit it, so it might be something to do with the HTML of it that is making it not work? Thanks for the help so far by the way, it's really appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-941072 Share on other sites More sharing options...
PFMaBiSmAd Posted October 21, 2009 Share Posted October 21, 2009 There is a typo in the second "visbile" - <input type="radio" name="visible" value="0" /> No <input type="radio" name="visbile" value="1" checked /> Yes Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-941077 Share on other sites More sharing options...
j05hr Posted October 21, 2009 Author Share Posted October 21, 2009 I'm now getting this error message, ini_set("display_errors", "1"); error_reporting(E_ALL); Update query failed, query: UPDATE subjects SET menu_name = 'About Widget Cor', position = 1, visible = '1 WHERE id = 1 Rows: -1 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1 WHERE id = 1' at line 4 Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-941081 Share on other sites More sharing options...
PFMaBiSmAd Posted October 21, 2009 Share Posted October 21, 2009 A) You apparently put the two lines of code setting display_errors/error_reporting outside of any of the <?php ?> tags - Also, add the following two lines of code immediately after your first opening <?php tag (move the require_once(...) statement down as necessary) - B) Your query either has an extra single-quote before '{$visible} or it is missing the matching single-quote after that. Since the value is numeric, it is likely that the leading single-quote is not intended. Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-941088 Share on other sites More sharing options...
j05hr Posted October 21, 2009 Author Share Posted October 21, 2009 Thanks a lot, that fixed it, the stupid video had one obviously that he took out while not recording it. Quote Link to comment https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/#findComment-941094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.