-1

Hello i am a beginner on php and stuck on this moment. i can not find the problem.

Notice: Undefined index: post_id in C:\XA\htdocs\Passie Blog\post.php on line 22

this is line 22 where the problem should be:

$id = $_POST['post_id'];

this my php code

<?php
if(!isset($_GET['id'])){
    header('Location: index.php');
    exit();
}else{
    $id = $_GET['id'];
}
include('includes/db_connect.php');
if(!is_numeric($id)){
    header('Location: index.php');
}
$sql = "SELECT title, body FROM posts WHERE post_id='$id'";
$query = $db->query($sql);
if($query->num_rows !=1){
    header('Location: index.php');
    exit();
}
if(isset($_POST['submit'])){
    $email = $_POST['email'];
    $name = $_POST['name'];
    $comment = $_POST['comment'];
    $id = $_POST['post_id'];
    if($email && $name && $comment){
        //
        $email = $db->real_escape_string($email);
        $name = $db->real_escape_string($name);
        $id = $db->real_escape_string($id);
        $comment = $db->real_escape_string($comment);
        if($addComment = $db->prepare("INSERT INTO comments(name, post_id, email_add, comment) VALUES (?,?,?,?)")){
            $addComment->bind_param('ssss', $id, $name, $email, $comment);
            $addComment->execute();
            echo "Bedankt! uw bericht is toegevoegd";
            $addComment->close();

        } else{
            echo "Error";
        }
    } else{
        echo "ERROR";
    }
}
?>

and this is the rest of my page

<div id="container">
    <div id="post">
        <?php
            $row = $query->fetch_object();
            echo "<h2>".$row->title."</h1>";
            echo "<p>".$row->body."</p>";
        ?>
    </div>
    <hr />
    <div id="add-comments">
        <form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
            <div>
                <label>Email Adres</label><input type="text" name="email" />
            </div>
            <div>
                <label>Naam</label><input type="text" name="name" />
            </div>
            <div>
                <label>Commentaar</label><textarea name="comment"></textarea>
            </div>
            <input type="hidden" name="post_id" value="<?php echo $id?>" />
            <input type="submit" name="submit" value="Toevoegen"/>
        </form>
        </div>
        <hr />
        <div id="comments">
        <?php
            $query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
            while($row = $query->fetch_object()):
        ?>
            <div>
                <h5><?php echo $row->name?></h5>
                <blockquote><?php echo $row->comment?></blockquote>
            </div>
        <?php endwhile;?>
        </div>
</div>
user2619538
  • 1
  • 1
  • 3

2 Answers2

1

No field with the name post_id eists in your form. You are however passing the ID manually through the URL in your form action. To get the ID, you would use $_GET['id'] rather than $_POST['post_id']

PlausibleSarge
  • 2,163
  • 1
  • 12
  • 12
0

The variable post_id isn't set (in post anyway). I'd change these

$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$id = $_POST['post_id'];

to something like

$email = !empty($_POST['email']) ? $_POST['email'] : '';
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$comment = !empty($_POST['comment']) ? $_POST['comment'] : '';
$id = !empty($_POST['post_id']) ? $_POST['post_id'] : '';

This way you have a fall-back value if the form isn't completely filled out.

JimL
  • 2,501
  • 1
  • 19
  • 19
  • Done this! thx for it I have put this code to the form. that helps for the problem but know i dont see any comments on the page have any idea? – user2619538 Jul 25 '13 at 23:46