-3

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 -->
Sam
  • 2,856
  • 3
  • 18
  • 29
  • select($query) -> fetch_assoc(); $query = "SELECT * FROM catagory"; $catagory = $db->select($query); ?> – James McClelland Jul 13 '17 at 15:31
  • What is this code in the comment? Can you elaborate what is the purpose for it? – Sam Jul 13 '17 at 15:38
  • You must post all the code in order to receive any answers because as it is, it is unclear what you want to achieve **Edit:** Make sure that you have a field with the name **post_id** in your form when submitting it. – rob_ Jul 13 '17 at 15:32
  • Try this to see what is going wrong , do not see all of the code in here. – Rajiv S Mehta Jul 13 '17 at 15:35

2 Answers2

1

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

Sam
  • 2,856
  • 3
  • 18
  • 29
  • This is correct mostly, except it wouldn't throw a fatal error - it would throw a notice - the code would still execute it just won't work properly. – GrumpyCrouton Jul 13 '17 at 15:47
  • My thought is that for this case the variable is needed for the code to run; no `$id` - or wrong id - will also stop the script because it is needed for the query? If the query is wrong then nothing that he is trying to do here would work? – Sam Jul 13 '17 at 15:50
  • Your right it wouldn't work, but it wouldn't throw a fatal error (at least not from the $id variable) - the page would continue to execute even if the variable isn't set, there would just be no output from the database. Check my answer for the way I fixed that issue – GrumpyCrouton Jul 13 '17 at 15:52
  • It's a good answer, I made a suggestion – Sam Jul 13 '17 at 16:06
1

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>
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
  • Instead of killing the script having a fall back seems like a more user-friendly route... Like sending them to homepage, or a random post – Sam Jul 13 '17 at 16:05
  • @Samuel I agree with you. I just used `die()` as I don't know OP's server set up or what OP would like to happen, but it can easily be changed out with any solution OP would like – GrumpyCrouton Jul 13 '17 at 16:07