"Edit" function on Blog posts

Ask about a PHP problem here.
Post Reply
Chlor
Posts: 9
Joined: Fri Dec 07, 2012 12:56 pm
Location: Sweden

"Edit" function on Blog posts

Post by Chlor »

Hello, I have followed the Blog tutorial on YouTube and it has worked great, it really helped me out! :)

However, I have recently tried to add an "Edit" function to the blog posts, so that I can come back and edit them with ease and would think that my code should work. I can't find any errors (mysql_error(); gives me nothing) but my database refuses to update, for whatever reason. I found an old thread with a similar problem, but sadly that didn't help.

Here's the "Edit" site.
<?php
include('core/init.php');

 
if (isset($_POST['title'], $_POST['body'])){
	edit_post($_POST['title'], $_POST['body']);
	header('Location: work.php');
	die();
	}
 
?>
 
<!DOCTYPE html>
<head>
<title>Edit</title>
</head>
 
	<body>
		<div>
			<?php
	 
			if (isset($_GET['pid']) === false || valid_pid($_GET['pid']) === false){
				echo 'Invalid post ID.';
			}
			else {
				$post = get_post($_GET['pid']);
			?>
			
			<h2><?php echo $post['title']; ?></h2>
			<h4><?php echo $post['date']; ?></h4>
			<hr />
			
			<p><?php echo $post['body']; ?></p>
			
			<hr />
	 
	 
	 
			<form action="" method="post">
				<p>
					<label for="title">Title</label>
					<input type="text" name="title" id="title" value="<?php echo $post['title']; ?>"/>
				</p>
				<p>
					<textarea name="body" rows="20" cols="60"><?php echo $post['body']; ?></textarea>
				</p>
				<p>
					<input type="submit" value="Edit Post" />
				</p>
			</form>
	<?php
	}
	?>

		</div>
	</body>
</html>
and the function to go with it.
function edit_post($title, $body){
	$pid = $_POST['id'];
	$title	= mysql_real_escape_string(htmlentities($title));
	$body	= mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("UPDATE `posts` SET `post_title` = '{$title}', `post_body` = '{$body}' WHERE `post_id` = '{$pid}'");
}
Any help would be extremely helpfull!
Last edited by Chlor on Sat Dec 08, 2012 5:11 pm, edited 1 time in total.
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: "Edit" function on Blog posts

Post by ExtremeGaming »

Add an extra parameter in your edit_post function to store the $_POST['id'] I don't think once inside the function you can post data like that

$_POST['id'] will return blank and thus there will cause no error
<?php while(!$succeed = try()); ?>
Chlor
Posts: 9
Joined: Fri Dec 07, 2012 12:56 pm
Location: Sweden

Re: "Edit" function on Blog posts

Post by Chlor »

Sadly that didn't work, I have continued to turn this over and around but no luck, I must be missing something else. My PHP skills are pretty limited so there can be all kinds of stuff done wrong.

This is what you meant for me to do right? Simply add the $pid to the parameters?
function edit_post($title, $body, $pid){

	$pid	= $_POST['id'];
	$title	= mysql_real_escape_string(htmlentities($title));
	$body	= mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("UPDATE `posts` SET `post_title` = '{$title}', `post_body` = '{$body}' 'WHERE `post_id` ='{$pid}'");
I still don't get an error, but it will still not update the database.

EDIT: Alright, I realised that my "header ('Location') " part made me jump past the errors that were displayed in the edit-file when I tried to submit my update. Bergh.

However, now I have two undefined index errors regarding the $_POST['id'] in the above function and in my
if (isset($_POST['title'], $_POST['body'])){
	edit_post($_POST['title'], $_POST['body'], $_POST['id']);
	die();
	}
Where do I define the 'id' index?

Another strange thing that I don't understand is that if I enter the $_POST['id'] variable into my "if" clause, I stop getting errors, but nothing will update and my "header ( 'Location')" will stop working, why is this?

Any help would be greatly appreciated.
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: "Edit" function on Blog posts

Post by ExtremeGaming »

Remove the $pid = $_POST['id'] as it will always return blank because your form contains no field of id. Remove it from the if statement as well.

I believe what you are looking for is $post['id'] for the function variable instead of $_POST['id'];

However, to get this to work your form action will need to change as well. Post the get_post() function please and that will clarify things.
<?php while(!$succeed = try()); ?>
Chlor
Posts: 9
Joined: Fri Dec 07, 2012 12:56 pm
Location: Sweden

Re: "Edit" function on Blog posts

Post by Chlor »

Oh, alright. so now I have changed the $_POST with $post and added $post to my "if (isset())" clause, where it remains undefined.

This is the get_post function that I use. It's unedited from when I first created it using the blog tutorial.
function get_post($pid){
	
	$pid = (int)$pid;
	
	$sql = "SELECT
				 `post_title` AS `title`,
				 `post_body` AS `body`,
				 `post_user`AS `user`,
				 `post_date` AS `date`
			FROM `posts`
			WHERE`post_id`= {$pid}";
			
	$post = mysql_query($sql);
	$post = mysql_fetch_assoc($post);
	
	$post['comments'] = get_comments($pid);
	
	return $post;
}

The edit_post function:
function edit_post($title, $body, $pid){

	$pid	= $post['id'];
	$title	= mysql_real_escape_string(htmlentities($title));
	$body	= mysql_real_escape_string(nl2br(htmlentities($body)));
	
	
	mysql_query("UPDATE `posts` SET `post_title` = '{$title}', `post_body` = '{$body}' WHERE `post_id` ='{$pid}'");
and my if(isset());
if (isset($_POST['title'], $_POST['body'], $post['id'])){
	edit_post($_POST['title'], $_POST['body'], $post['id']);
	die();
	}
	
	echo mysql_error();
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: "Edit" function on Blog posts

Post by ExtremeGaming »

Yes, you're going to have to change your form action to
<form action="?pid=<?php echo $_GET['pid']; ?>"
Then your if():
if (isset($_POST['title'], $_POST['body'], $_GET['pid'])){
        edit_post($_POST['title'], $_POST['body'], $_GET['pid']);
        die();
        }
Then your function:
function edit_post($title, $body, $pid){
 
        $pid    = mysql_real_escape_string($pid);
        $title  = mysql_real_escape_string(htmlentities($title));
        $body   = mysql_real_escape_string(nl2br(htmlentities($body)));
       
       
        mysql_query("UPDATE `posts` SET `post_title` = '{$title}', `post_body` = '{$body}' WHERE `post_id` ='{$pid}'");
Something like that will (should) work
<?php while(!$succeed = try()); ?>
Chlor
Posts: 9
Joined: Fri Dec 07, 2012 12:56 pm
Location: Sweden

Re: "Edit" function on Blog posts

Post by Chlor »

It works perfectly. Thanks a lot for your help, you're a lifesaver, bro. I've spent hours upon end scratching my head while searching the net for a solid answer to this.

Are answered threads like these locked, should I request one?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: "Edit" function on Blog posts

Post by Helx »

Chlor wrote:Are answered threads like these locked, should I request one?
No, but if you'd like?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: "Edit" function on Blog posts

Post by jacek »

The new forum will have a official solved marker so please don't edit it into the title for now :)
Image
Post Reply