Question

So I am creating a blog for a school project, what I want to do is to be able the click on a blog post on the start page and be redirected to a separate page where only that blog post is displayed. What I have tried is this:

START PAGE LINK:

<a class="blog_title_link" href="singlepost?ID=' . $row['postID'] . '">

AND THE OTHER PAGE:

$pID = -1;
if ($_GET['ID'] != NULL) {
    $pID = $_GET['ID'];
} else {
    echo "ID failed.";
    //Redirect to startpage...
}

$con = mysqli_connect("localhost","root","hejsan123","MyStrength");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}    

$result = mysqli_query($con,"SELECT * FROM posts WHERE postID=") . $pID;

while($row = mysqli_fetch_array($result))
{
    echo '<h1 class="blogTitle"><a class="blog_title_link">' 
   . $row['title'] . '</a></h1><img src="yo.jpg" /><p class="content_box_p">' 
   . $text . '</p>';
   echo "<br>";
}               
mysqli_close($con);

So I have the $pID to check if there actually is a ID, and then I try to get it from the URL so that I can use it in the SQL to choose which information I want from the database. What happens rigth now is

404 not found.

Hope you understand! Thanks!

Was it helpful?

Solution

the 404 Not Found error could it be because of the link:

<a class="blog_title_link" href="singlepost?ID=' . $row['postID'] . '">
                                 __________^___      

Maybe you need singlepost.php?ID...?

And then this line too should be:

$result = mysqli_query($con,"SELECT * FROM posts WHERE postID=" . $pID); 
//                                                       ___not^___but^         
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top