php - Notice: Undefined variable when creating posts
I'm pretty new to PHP and MySQL and I just can't figure this one out. I have searched all around the forum but haven't found an answer. The problem is then I delete all posts from CMS it shows the following error:
There I made mistake?
Thank you in advance for all your help!
My Source code:
<?php
$query = "SELECT * FROM posts";
$select_posts = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_posts)) {
$post_id = $row['post_id'];
$post_author = $row['post_author'];
$post_title = $row['post_title'];
$post_category_id = $row['post_category_id'];
$post_status = $row['post_status'];
$post_image = $row['post_image'];
$post_tags = $row['post_tags'];
$post_comment_count = $row['post_comment_count'];
$post_date = $row['post_date'];
}
echo "<tr>";
echo "<td>$post_id</td>";
echo "<td>$post_author</td>";
echo "<td>$post_title</td>";
$query = "SELECT * FROM categories WHERE cat_id = {$post_category_id} ";
$select_categories_id = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_categories_id)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<td>{$cat_title}</td>";
}
echo "<td>$post_status</td>";
echo "<td><img src='../images/$post_image' width='150' height='50'></td>";
echo "<td>$post_tags</td>";
echo "<td>$post_comment_count</td>";
echo "<td>$post_date</td>";
echo "<td><a href='posts.php?source=edit_post&p_id={$post_id}'>Edit</a></td>";
echo "<td><a href='posts.php?delete={$post_id}'>Delete</a></td>";
echo "</tr>";
?>
Answer
Solution:
If you have deleted all the records then you should do a logical test to check there are some records before trying to echo them back to screen.
Answer
Solution:
You have to put the whole code into the while loop, because this depends on the result of the query.