-1

Notice: Undefined variable: the_post_id in C:\xampp\htdocs\renda\post.php on line 27

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\renda\post.php on line 30

This is line 27

$query ="SELECT * FROM posts WHERE post_id = {$the_post_id}";

This is line 30

while ($row = mysqli_fetch_assoc($select_all_posts_query)) {

This is my PHP Code

<?php

if(isset($_GET['p_id'])) {
    $the_post_id = $_GET['p_id'];
}


$query ="SELECT * FROM posts WHERE post_id = {$the_post_id}";
$select_all_posts_query = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($select_all_posts_query)) {
    $post_id = $row['post_id'];
    $post_title = $row['post_title'];
    $post_author = $row['post_author'];
    $post_date = $row['post_date'];
    $post_image = $row['post_image'];
    $post_content = $row['post_content'];
Community
  • 1
  • 1
Morph9090
  • 105
  • 1
  • 3
  • 6

1 Answers1

-1

The first "notice" error can be prevented by not executing all the code if p_id isn't present. The second error is because mysqli_query can return false (see the PHP documentation for the function).

I would probably do something like this:

<?php

    if (isset($_GET['p_id'])) {

        $the_post_id = $_GET['p_id'];

        $query = "SELECT * FROM posts WHERE post_id = $the_post_id";
        $select_all_posts_query = mysqli_query($connection, $query);

        if ($select_all_posts_query) {
            while ($row = mysqli_fetch_assoc($select_all_posts_query)) {
                $post_id = $row['post_id'];
                $post_title = $row['post_title'];
                $post_author = $row['post_author'];
                $post_date = $row['post_date'];
                $post_image = $row['post_image'];
                $post_content = $row['post_content'];
            }
        }
        else {
            // handle error condition (the query failed)
        }
    }
    else {
        // handle p_id not being in $_GET
    }

?>
Davis
  • 856
  • 4
  • 11