Jump to content

Help with php/sql tutorial from Lynda


ozoned

Recommended Posts

Hi, all.  I need some help figuring out why something I"m following along with from Lynda isn't working.  The problem: when I try to "update" something in my table using single-page submission, my page flashes once and everything goes back to the way it was, no insert. The table is in no way modified -- nothing goes into it.

 

Here is the page: http://www.skullengine.com/tutorial/edit_subject.13.11.php.  When you click on one of the NON-BULLETED LINKS ONLY you should be able to change the title, hit "edit subject" and have it change in the sidebar. Note that because so much dynamic data is displaying, contact is being made with the table.

 

I built this right along with Lynda.com's tutorials, so I'm stumped finding the problem.

 

Page Code (minus connection that works, header and footer information that works, tell me if you need it):

 

<?php connect(); 

	if (intval($_GET['subj']) == 0) {
		redirect_to("content.138.php");
	}
	if (isset($_POST['submit'])) {
		$errors = array();

		$required_fields = array('menu', 'position', 'visible');
		foreach($required_fields as $fieldname) {
			if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) { 
				$errors[] = $fieldname; //puts at the end of the array
			}
		}
		$fields_with_lengths = array('menu' => 30);
		foreach($fields_with_lengths as $fieldname => $maxlength ) {
			if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; }
		}

		if (empty($errors)) {
			// Perform Update
			$tid = mysql_prep($_GET['subj']);
			$menu = mysql_prep($_POST['menu']);
			$position = mysql_prep($_POST['position']);
			$visible = mysql_prep($_POST['visible']);

			$query = "UPDATE subjects SET 
						menu = '{$menu}', 
						position = {$position}, 
						visible = {$visible} 
					WHERE id = {$tid}";
			$result = mysql_query($query);
			confirm_query($result);
			if (mysql_affected_rows() == 1) {
				// Success
			} else {
				// Failed
			}

		} else {
			// Errors occurred
		}

	} // end: if (isset($_POST['submit']))

find_selected_page();
?>
<?php include("includes/header.php"); ?>
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page); //we echo this because we are using $output to handle text, not echoing each line in the function ?>

<!-- ************************** CONTENT AREA ***************************** -->
</td>
<td id="page">

<h2>Edit Subject: <?php echo $sel_subject['menu']; ?></h2><br /> <!-- gets sel_sucject from Find_selected_page above -->

<!-- ************************ FORM ************************* -->

		<form action="edit_subject.13.11.php?subj=<?php echo urlencode($sel_subject['id']); ?>" method="post">
			<p>Subject name: 
				<input type="text" name="menu" value="<?php echo $sel_subject['menu']; ?>" id="menu" />
			</p>
			<p>Position: 
				<select name="position">
					<?php
						$subject_set = get_all_subjects();
						$subject_count = mysql_num_rows($subject_set);
						// $subject_count + 1 b/c we are adding a subject
						for($count=1; $count <= $subject_count+1; $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="visible" value="1"<?php 
				if ($sel_subject['visible'] == 1) { echo " checked"; } 
				?> /> Yes
			</p>
			<input type="submit" name="submit" value="Edit Subject" />
		</form><br /><br />
<!-- *********************  end form ********************************** -->
<?php echo "<a href=\"" . rawurlencode(CONTENT_PAGE) ."\"> Cancel</a>"; ?>
</td>
</tr>
</table>
</div>


 

Functions

<?php



//redirect_to($location)
function redirect_to($location = NULL) {
if ($location != NULL) {
header("Location: {$location}");
exit; //exit with all headers
}
}

//Collects Data from divs, populates sidebars accordingly
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;
}	
}	

//Connection function  (This works, the constants just hide the specifics and are in another file.)
function connect() {
require("constants.connect.php");
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);  
//($connection works, but can't be called in queries or it calls errors. I might be using it wrong or not declaring it, but I seem to //conduct queries fine without directly declaring it)
//if (!$connection) {
//echo("Database initial CONNECTION failed line 24: " . mysql_error());
//} else {echo "Database initial CONNECTION works ";} 
$db_select = mysql_select_db(DB_NAME);//$connection;
//if (!db_select) {
//echo ("Database initial SELECTION failed: ". mysql_error() ."<br /><br />");
//} else {echo "<br /><br />Database  initial SELECTION works<br /><br />";
//}
return $connection;
}

//Confirm query
function confirm_query($result_set) {
if (!$result_set) {
echo("<br /><br />Database QUERY FAILED, nothing is passing into this funciton. " . mysql_error() . "<br />");
}
}



//subject navigation
function get_all_subjects() {
$query = "SELECT * 
		FROM subjects 
		ORDER BY position ASC";
$subject_set = mysql_query($query);
/*if (!$subject_set) { 
//echo "<br /><br />Subject Set Query Unsuccessful for NAVIGATION<br /><br />";
//} else {
//echo "<br /><br />Subject Set Query SUCCESSFUL for NAVIGATION<br /><br />";
}*/
confirm_query($subject_set);
return $subject_set;
}



//page navigation
function get_pages_for_subject($subject_id){
$query="SELECT * 
		FROM pages 
		WHERE subject_id = {$subject_id} 
		ORDER BY position ASC";
$page_set = mysql_query($query);
/*if (!$page_set) { 
echo "<br /><br />Page Set Query Unsuccessful for NAVIGATION<br /><br />";
} else {
echo "<br /><br />Page Set Query SUCCESSFUL for NAVIGATION<br /><br />";
}*/
confirm_query($page_set);
return $page_set;
}


//gather subjects form table
function get_subject_by_id($subject_id) {
//global $connection;
$query = "SELECT *";
$query .= " FROM subjects ";
$query .= " WHERE id={$subject_id}";
$query .= " LIMIT 1";
/*echo "<br />" . $query . "<br />"; test to make sure we are getting something */
$result_set = mysql_query($query);
confirm_query($result_set);
/*if(!confirm_query){
echo("<br />Confirm Query failed: " . mysql_error());
}
if(!$result_set){
echo("<br />Result_set failed: " . mysql_error() . "<br /><br />");
}*/
//above gathered the actual subjects, remember spaces in query statement
//now we put the result set in a single row, we don't need a whole set...
if($subject = mysql_fetch_array($result_set)) {
//remember that if $subject is a number not in the database, like 500, it will return FALSE instead of a number.
return $subject;
} else {
echo("<br />Mysql_fetch_array failed: " . mysql_error() . "<br /><br />");
}
}

//gather pages form table
function get_page_by_id($page_id) {
//global $connection;
$query = "SELECT *";
$query .= " FROM pages ";
$query .= " WHERE id={$page_id}";
$query .= " LIMIT 1";
/*echo "<br />" . $query . "<br />"; test to make sure we are getting something */
$result_set = mysql_query($query);
confirm_query($result_set);
/*if(!confirm_query){
echo("<br />Confirm Query failed: " . mysql_error());
}
if(!$result_set){
echo("<br />Result_set failed: " . mysql_error() . "<br /><br />");
}*/
//above gathered the actual subjects, remember spaces in query statement
//now we put the result set in a single row, we don't need a whole set...
if($page = mysql_fetch_array($result_set)) {
//remember that if $subject is a number not in the database, like 500, it will return FALSE instead of a number.
return $page;
} else {
echo("<br />Mysql_fetch_array for page failed: " . mysql_error() . "<br /><br />");
}
}

//Cleaning function from Lynda.com
function mysql_prep($value) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists ( "my_sql_real_escape_string" );
if ($new_enough_php) {
if ($magic_quotes_active) { 
$value = stripslashes ( $value );
$value= mysql_real_escape_string( $value );
} else {
if (!$magic_quotes_active) { 
$value = addslashes( $value ); }
}
return $value;
}
}

// Navigation
function navigation($sel_subject, $sel_page){
$output = "<ul class=\"subjects\">";
$subject_set = get_all_subjects();
while ($subject = mysql_fetch_array($subject_set)) {
	//$subject["id"] is the subject id in the returned table array
	//SIDEBAR highlight link depending on the page we are on
	$output .= "<li";
		if ($subject["id"] == $sel_subject['id']){
		$output .= " class=\"selected\" ";
		 }
	 $output .= "><a href=\"".rawurlencode(EDIT_SUBJECT)."?subj=" . urlencode($subject["id"]) ."\">{$subject["menu"]}</a></li>";
	//page SIDEBAR navigation
	$page_set = get_pages_for_subject($subject["id"]);
	$output .= "<ul class=\"pages\">";
	while ($page = mysql_fetch_array($page_set)) { // REMEMBER: $page just holds the array from the table as an array
		$output .= "<li ";
			if ($page["id"] == $sel_page['id']) { 
				$output .= "class=\"selected\" "; }  // REMEMBER: $sel_page is the page GET value
		$output .= "><a href=\"".rawurlencode(CONTENT_PAGE)."?page=". urlencode($page["id"]). "\">{$page["menu"]}</a></li>";
		}
		$output .= "</ul>";
	}
$output .= "</ul>";
return $output;
}


?>

Link to comment
Share on other sites

I thought it might be something in that line, too, but the first of the two ways threw an error (something about not being allowed to read [] in that way) and the second line unfortunately produced no change in results.

 

The second way is currently in place, note the error it is throwing as a result of a debugging statement...Does this provide any additional clues?

Link to comment
Share on other sites

yes, i was referring to my signature and the previous post.

 

your problem is at least partially here:

 

	if (intval($_GET['subj']) == 0) {
		redirect_to("content.138.php");
	}
	if (isset($_POST['submit'])) {

 

if you POST the form, $_GET['subj']) == 0 is true. therefore, if you POST the form, you will get redirect_to().

Link to comment
Share on other sites

That confused me too because it is not redirecting -- the content page is different and isn't showing up here, this page keeps returning to itself. 

 

Maybe it would help to explain that the $_GET us not as a result of the form that is posted, it's a result of choosing one of the navigational links.

 

// Navigation
function navigation($sel_subject, $sel_page){
$output = "<ul class=\"subjects\">";
$subject_set = get_all_subjects();
while ($subject = mysql_fetch_array($subject_set)) {
	//$subject["id"] is the subject id in the returned table array
	//SIDEBAR highlight link depending on the page we are on
	$output .= "<li";
		if ($subject["id"] == $sel_subject['id']){
		$output .= " class=\"selected\" ";
		 }
	 $output .= "><a href=\"".rawurlencode(EDIT_SUBJECT)."?subj=" . urlencode($subject["id"]) ."\">{$subject["menu"]}</a></li>";
	//page SIDEBAR navigation
	$page_set = get_pages_for_subject($subject["id"]);
	$output .= "<ul class=\"pages\">";
	while ($page = mysql_fetch_array($page_set)) { // REMEMBER: $page just holds the array from the table as an array
		$output .= "<li ";
			if ($page["id"] == $sel_page['id']) { 
				$output .= "class=\"selected\" "; }  // REMEMBER: $sel_page is the page GET value
		$output .= "><a href=\"".rawurlencode(CONTENT_PAGE)."?page=". urlencode($page["id"]). "\">{$page["menu"]}</a></li>";
		}
		$output .= "</ul>";
	}
$output .= "</ul>";
return $output;]

Link to comment
Share on other sites

Wrong information is often worse than no information.

 

Why are you getting all cocky. I gave no wrong information. I told him where his problem may lie, and I gave him two things to try to see if it did.

 

So feel free to go screw yourself.

Link to comment
Share on other sites

That confused me too because it is not redirecting -- the content page is different and isn't showing up here, this page keeps returning to itself.

 

The poster before you said:

 

if you POST the form, $_GET['subj']) == 0 is true.

 

But if you look at the action of the form:

 

<form action="edit_subject.13.11.php?subj=<?php echo urlencode($sel_subject['id']); ?>" method="post">

 

Subject is in fact set there.

 

Please forgive him, apparently he hasn't realized that wrong information is often worse than no information.

 

 

Link to comment
Share on other sites

Lets find out where your code is going wrong. Change this:

 

				if (mysql_affected_rows() == 1) {
				// Success
			} else {
				// Failed
			}

		} else {
			// Errors occurred
		}

	} // end: if (isset($_POST['submit']))

 

To this:

 

				if (mysql_affected_rows() == 1) {
				echo "Success";
			} else {
				echo  "Failed";
			}

		} else {
			echo "Errors occurred";
		}

	} // end: if (isset($_POST['submit']))
exit;

 

You will either get a blank screen, or you will get one of 'Success', 'Failed' or 'Errors occurred'. Post back here with which one you get.

Link to comment
Share on other sites

It returns a blank screen. 

 

I tried it directly and it was blank. I then started with the page that leads to this one, selected a navigational link that would lead to this page, then the page still presented as blank.

Link to comment
Share on other sites

That means that for some reason, isset($_POST['submit'])) is returning false. I'm honestly not seeing why though. It seems to me that it should.

 

What is the name of the script you are working on? Is it 'edit_subject.13.11.php'?

Link to comment
Share on other sites

It was, but the one with your change is edit_subject.13.12.php -- I'm keeping it sequential so I can track bugs.

 

I don't know if this helps, but when I do not use $connection things work better than if I do.  For example, if I were to write $result = mysql_query($query) as $result = mysql_query($query, $connection);  I get the error

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/.wakanda/oxoned/skullengine.com/tutorial/edit_subject.13.12.php on line 35

AND YOUR TEST RETURNS "FAILED."  I mention this because it is not a blank page as before.  Also, the table populates, so it is actually functioning on other pages where we do not have the issues we are having here.  You are right, checking for the 'submit' post is new to this portion of the tutorial.

  • It's currently set up with the $connection on edit_subject.13.12.php so you can see.
  • edit_subject.13.11.php is the file as it was before we tested anything.

Link to comment
Share on other sites

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.