Jump to content

Recommended Posts

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;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/178398-solved-editing-a-page/
Share on other sites

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?

 

 

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 /> 

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.

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.

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.

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.