Please don't take the following personally.
Actually, you want to get messages like this one, which will occur when $post_id isn't set properly in line 41, although you may not want to show them to the public ... more on that (and that) later.
But there's so many problems with this script, I would suggest, PLEASE DO NOT USE IT. From the attached screenshots, I take it you love Jesus ... please don't put this code out there in His Name. 😉
For example, this assignment:
//getting post_id from url
$post_id = $_GET['post_id'];
We're getting a variable from the URL and NOT SANITIZING IT? We are going to get our database (and maybe our entire server) pwned in 4 minutes and 15 seconds[SUP]*[/SUP] after this goes public.
Obligatory XKCD cartoon: https://xkcd.com/327/
That's known as SQL injection, and serious students of PHP programming have been attempting to make sure we don't allow our scripts to be vulnerable to that for a long time[SUP]**[/SUP]. Shoot, brand new users of PHP have been attempting ... well ... you see my point, I hope.
Next, the error, as mentioned above. Most of us know that when you query the database, you hope to get a $result from a call to mysqli_query, but you might not get a result, and therefore you need to handle that possibility. This script doesn't care about that at all ... look:
$result = mysqli_query($connect, "SELECT * FROM posts WHERE post_id=$post_id");
while($res = mysqli_fetch_array($result)) {
$user_id = $res['user_id'];
$topic_id = $res['topic_id'];
$post_title = $res['post_title'];
$post_content = $res['post_content'];
}
What do you see here? We call "mysqli_fetch_array()" on the $result variable without bothering to check if it's actually a valid result. That's the source of your error above.
It should be more like:
$result = mysqli_query($connect, "SELECT * FROM posts WHERE post_id=$post_id");
if ($result && $result->num_rows) {
while($res = mysqli_fetch_array($result)) {
$user_id = $res['user_id'];
$topic_id = $res['topic_id'];
$post_title = $res['post_title'];
$post_content = $res['post_content'];
}
} else {
echo "We have no such story!"; //a simple example of what should be a very nice looking error page.
}
As a matter of fact, just about everything about this script screams 'it's still 2004 where we live'. It's not! :eek:
In my profession opinion, you should not use the script until it's cleaned up and properly secured. I will add "Please" ... 🙂
[SUP]*[/SUP]possibly quite less
[SUP]**[/SUP]as evidence, I note 7.42 MILLION results for "php mysql injection" on a major search engine....