"Edit" function on Blog posts
Posted: Fri Dec 07, 2012 1:35 pm
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.
[syntax=php]<?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>[/syntax]
and the function to go with it.
[syntax=php]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}'");
}[/syntax]
Any help would be extremely helpfull!
![Smile :)](./images/smilies/icon_e_smile.gif)
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.
[syntax=php]<?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>[/syntax]
and the function to go with it.
[syntax=php]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}'");
}[/syntax]
Any help would be extremely helpfull!