Hi,

I recently begin to work with MYSQL, php and html and I have this problem. On the homepage of my site on the sidebar I have a section where it tells you the latest blogs that have been posted. Now here comes the problem. I wish that whenever I click of the blog it opens the blog page with the right content of the posted clicked.

My problem is how I am going to link the title on the homepage with the content of the blog on another page.

Thank you
Luca Tabone

Dani AI

Generated

A minimal, reliable pattern: write each blog title on the homepage as a link that carries a unique identifier (an integer id or an SEO slug), then let a single detail page read that identifier, query the database for the matching row, and render the stored title and content. Use PDO or MySQLi with prepared statements, validate/cast the incoming parameter, and be explicit about whether stored content includes HTML (sanitize when needed).

Example listing (homepage):

<?php
// index.php — listing latest titles
$stmt = $pdo->query('SELECT id, title FROM posts ORDER BY created_at DESC LIMIT 5');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  echo '<a href="post.php?id=' . (int)$row['id'] . '">' . htmlspecialchars($row['title']) . '</a><br>';
}
?>

Example detail page (post.php):

<?php
// post.php — show single post
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$stmt = $pdo->prepare('SELECT title, content FROM posts WHERE id = ?');
$stmt->execute([$id]);
$post = $stmt->fetch(PDO::FETCH_ASSOC);
if ($post) {
  echo '<h1>' . htmlspecialchars($post['title']) . '</h1>';
  // If content is trusted HTML (admin-entered), echo raw; otherwise sanitize:
  echo $post['content']; // or: echo nl2br(htmlspecialchars($post['content']));
} else {
  echo 'Post not found';
}
?>

Notes and troubleshooting:

  • ’s directory/ID questions matter only for link paths and parameter names; keep the query string consistent (e.g., ?id=) or use slugs with rewrite rules.
  • : if WordPress is in use, rely on its permalink/template functions instead of rolling this.
  • : since no code was posted, the above pattern covers the common gap.
  • Security: never interpolate raw GET values into SQL; prefer prepared statements. If stored content contains HTML from users, sanitize with a library (HTMLPurifier) before output. If older mysql_* functions are present, migrate to PDO/MySQLi (mysql extension removed in modern PHP).

Recommended Answers

All 3 Replies

Few questions for you;

(1) Are the blogs in the same directory as the index page? For example, if you have www.domain.com/index.php, are the blogs at www.domain.com/blogs/1.php?
(2) I would imagine the blogs have some sort of postID and/or blogID? If so, can you tell us which they have.
(3) Can you post the code you are using from the homepage?

Member Avatar for Member #120589

I agree with jr - post your code. You can't expect any help with such an obscure plea.

Are you using blog framework e.g. wordpress?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.