0

Thanks in advance.

I am trying to update information in my db but am having no luck and no error messages.

Here is the page code (the pid or post_id is in the URL):

<?php
include('page with functions.php');


if (isset($_GET['pid'], $_POST['title'], $_POST['body'])){

if (edit_post($_GET['pid'], $_POST['title'], $_POST['body'])){
header("Location: blog_edit.php?pid={$_GET['pid']}");
}else{
header("Location: some location.php");
}

die();

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Blog 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>


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

Here is the function (the pid or post_id is in the URL):

// edits a blog entry
function edit_post($title, $body){

$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));

mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{body}' WHERE `post_id` = {$pid}");

}

Any help would be great thanks! FYI I am new to PHP MYSQL so please be kind.

2
  • mysql_query("UPDATE posts` (post_title, post_body) SET post_title = '{$title}' AND post_body = '{$body}' WHERE post_id = {$pid}");` I think you missed the dollar before body. Commented Nov 6, 2011 at 19:13
  • A dumb mistake, but still no such luck! Commented Nov 6, 2011 at 19:25

1 Answer 1

1
if (edit_post($_GET['pid'], $_POST['title'], $_POST['body'])){
header("Location: blog_edit.php?pid={$_GET['pid']}");
}else{
header("Location: some location.php");
}

You're passing three arguments in the function call, but the function definition has only 2 arguments.

function edit_post($title, $body){

$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));

mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{body}' WHERE `post_id` = {$pid}");

}

Also, return a boolean from the function.

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

$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));

$result = mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{body}' WHERE `post_id` = {$pid}");

echo $result;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Added the pid and still no luck.
what does mysql_query return? Could you echo the same and post the result?
Sorry Im very new to this, I have been going off of tutorials..how would i do that.
Nothing happened, it preformed the redirect from the headers. I removed if (edit_post($_GET['pid'], $_POST['title'], $_POST['body'])){ header("Location: blog_edit.php?pid={$_GET['pid']}"); }else{ header("Location: index.php"); } and when i clicked submit, it reloaded the page and it was blank.
From the PHP manual... if (!$result) { die('Invalid query: ' . mysql_error()); } Try that in your edit_post function in place of echo $result. At least you'll see if the Update is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.