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?

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'];

@JorgeM, 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 @JorgeM.

Thank you @lps. 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.