Hi everybody.
I have a page with some post's title in it. I have put every title in an <a> tag. I want to click on each title and enter into an other page (single-post-page.php) to see the content of that post's title.
The script of that page (single-post-page.php) should get that post's id when i click on the title's link to send it to the db and get that post's content to echo on the page.

But my problem is that how i can send post's id to that (single-post-page.php) page by clicking on each title's link?

Dani AI

Generated

Good call from and : pass the ID in the URL, then read it on the detail page. Two small upgrades will make this safer and cleaner: sanitize output and use prepared statements.

Example link builder (escapes output and forces a numeric id):

$link = 'single-post-page.php?id=' . (int)$row['ID'];
echo '<a href="' . htmlspecialchars($link, ENT_QUOTES, 'UTF-8') . '">'
   . htmlspecialchars($row['Title'], ENT_QUOTES, 'UTF-8')
   . '</a>';

On single-post-page.php, validate the input, use a parameterized query, and handle missing posts gracefully:

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$id) { http_response_code(404); exit('Post not found'); }

$dsn = "mysql:host=$servername;dbname=$dbname;charset=utf8mb4";
$conn = new PDO($dsn, $dbusername, $dbpassword, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

$stmt = $conn->prepare('SELECT ID, Title, Author, Content FROM Posts WHERE ID = ? LIMIT 1');
$stmt->execute([$id]);
$post = $stmt->fetch();

if (!$post) { http_response_code(404); exit('Post not found'); }

echo $post['ID'] . ') ' . htmlspecialchars($post['Title'], ENT_QUOTES, 'UTF-8');
echo '<br>by: ' . htmlspecialchars($post['Author'], ENT_QUOTES, 'UTF-8') . '<br>';
// If Content contains trusted HTML, echo it directly; otherwise escape it:
echo $post['Content'];

SEO note (since you tagged this): query strings are fine, but human-friendly URLs help CTR. If you are on Apache, you can add a simple rewrite so /post/123/ maps to your detail page without changing PHP:

RewriteEngine On
RewriteRule ^post/([0-9]+)/?$ single-post-page.php?id=$1 [L,QSA]

Recommended Answers

All 6 Replies

Ok. You stated the problem. Show the code you are trying to use.

The complete code of the page with titles:

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT ID, Title, Author, Content FROM Posts";
    $result = $conn->query($sql);

    echo "<table cellspacing='0' cellpadding='20' border='1px solid black' border-collapse='collapse'>";
    echo "<tr><th> POST ID </th><th> POST TITLE </th><th> POST AUTHOR </th><th> ACTION </th></tr>";

    foreach($result as $row) {
        echo "<tr><td>" . $row['ID'] . "</td><td> <a href='single-post-page.php'>" . $row['Title'] . "</a> </td><td>" . $row['Author'] . "</td><td> <a href='#'>Delete</a> <br> <a href='#'>Edit</a> </td></tr>";
    }
        echo "</table>";

} catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

<html>
<head>
<title>admin</title>
</head>
<body>
<hr>
<?php echo "<a href='writing-post.php'>writing new post</a>"; ?>
</body>
</html>

Here <a href='single-post-page.php'> is the part that should send it's id into single-post-page.php script.

And the code of single-post-page.php is:

<?php
$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT ID, Title, Author, Content FROM Posts WHERE ID = '2'";
    $result = $conn->query($sql);

    foreach($result as $row) {
        echo $row['ID'] . ") " .  $row['Title'];
        echo "<br>" . " by: " . $row['Author'];
        echo "<br>";
        echo $row['Content'];
        echo "<br><br><br>";
    }

} catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

<html>
<head>
<title>Single-post-page</title>
</head>
</html>

Of course here i have typed an id (number 2) myself just for example WHERE ID = '2'";, here is the part that should get the id by clicking on the post's title.

Just build the link and include a query string parameter that you can retrieve on the target page. For example..

 echo "<td><a href='single-post-page.php?post=" . $row['ID'] . "'>";

On the target page, retrieve the value passed.

 $_GET['post'];

, i edited all the title's links with this:
echo "<td><a href='single-post-page.php?post=" . $row['ID'] . "'>";

But what about $_GET['post'];? Where i should put it in the single-post-page.php script?

How i should edit this line?
$sql = "SELECT ID, Title, Author, Content FROM Posts WHERE ID = '2'";

modify the $sql = "SELECT ID, Title, Author, Content FROM Posts WHERE ID = '2'"; into $sql = "SELECT ID, Title, Author, Content FROM Posts WHERE ID = '".$_GET['post']."'";
or you can assign the $_GET['post'] into a variable first before use in query like

$id = $_GET['post'];
$sql = "SELECT ID, Title, Author, Content FROM Posts WHERE ID = '$id'";

Yes it works :)

Thank you .

Thank you . I tried WHERE ID = '".$_GET['post']."'"; first but it didn't work but the second way worked.

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.