不确定我错过了什么? [重复]

Could someone please help, I am not sure of what I am missing here.

This is the error I get:

Notice: Undefined index: post_id in C:\xampp\htdocs\projects\charlesprater\post.php on line 6

And this is my code as you can see below

<?php include "includes/header.php";?>

<?php
 $db = new Database();

  $id = $_GET['post_id']; 


  $query = "SELECT * FROM posts WHERE post_id = $id";

  $posts = $db->select($query) -> fetch_assoc();

  $query = "SELECT * FROM catagory";

  $catagory = $db->select($query);
  ?>



      <div class="blog-post">
        <h2 class="blog-post-title"><?php echo $posts['title'];?></h2>
        <p class="blog-post-meta"><?php echo formatDate($posts['date']);?> <a href="#"><?php echo $posts['author'];?></a></p>

        <p>
          <?php echo $posts['body'];?>
        </p>

      </div><!-- /.blog-post -->
</div>

You have not stated what the post_id is coming from:

Your variable statement is wrong:

$id = $_GET['post_id']; 

Change it to something like below:

if (isset($_GET['post_id']))
{
    $id = $_GET['post_id'];
}
else
{
    $id = "";
}

The reason is that the variable may not be stated in the URL param, and that will throw a fatal error to the page killing your script.

By adding the if statement you make sure that it is in fact there; if not id is set to NULL

Try this code:

<?php
    include "includes/header.php";
    $db       = new Database();

    //Use a ternary statement to check if $_GET['post_id'] is SET before using it. 
    //If it's not set, make the variable blank.
    $id       = isset($_GET['post_id']) ? $_GET['post_id'] : "";
    if(empty($id)) {
        //kill the page if ID was not set, the script should NOT continue with no ID.
        die("There was no ID set!");
    }

    $query    = "SELECT * FROM posts WHERE post_id = $id";
    $posts    = $db->select($query)->fetch_assoc();
    $query    = "SELECT * FROM catagory";
    $catagory = $db->select($query);
?>

<div class="blog-post">
    <h2 class="blog-post-title"><?php echo $posts['title'];?></h2>
    <p class="blog-post-meta"><?php echo formatDate($posts['date']);?> <a href="#"><?php echo $posts['author'];?></a></p>
    <p>
        <?php echo $posts['body'];?>
    </p>
</div>