SEO Friendly URL display same id on different address - PHP

Viewed 52

I have index.php, portfolio/index.php, portfolio/post.php and .htaccessfiles.

What is the status of the current code?

If you open my website example.com you will get a list of my portfolios. I have done small changes and gotten what I want. When the user clicks on my first added project, the user will be redirected to the directory portfolio and the URL will be for the project Example 2 -> example.com/portfolio/example-2.

What is the current problem?

I have now added my second project and give him a name Example 3 and now URL will be example.com/portfolio/example-3. The problem is when I open my newly added project, it will collect all details from my first entry or in this case from Example 2. Maybe the problem can be solved with added $_GET['project_id'] or something like that, but I need help with this.

Code for .htaccess:

RewriteEngine On
RewriteBase /portfolio/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ post.php?id=$1 [QSA,L]

Code for example.com/index.php -> This code is only for displaying all entries from a database and the second step is to open clicked project with all details.

<a href="portfolio/<?php echo htmlentities($project->link); ?>" title="<?php echo htmlentities($project->title); ?>">
    <span class="project__text"><?php echo htmlentities($project->title); ?></span>
</a>

NOTE:

In the database, $link is defined as URL of the project. So in this case $link data will get project slug example-3 or example-2.

Code for example.com/portfolio/index.php -> This page have same code as example.com/index.php is just for display all portfolios projects registered in the database.

Code for example.com/portfolio/post.php -> This page need's to collect all from specific ID / selected ID and display to visitors.

$link = null;
if(!empty($_GET['id'])) {
    $link = $_REQUEST['id'];
}
if (null === $link) {
    header('index');
} else {
    $sql = "SELECT * FROM projects WHERE link = :link";
    $query = $dbh->prepare($sql);
    $query->bindParam(':link', $link, PDO::PARAM_INT);
    $query->execute();
    $project = $query->fetch(PDO::FETCH_ASSOC);
    $cnt = 1;
}

Where can be a problem? If you maybe need information how I call let's say title or description from database, i use <?php echo $project['title'];?> to display project details in current page example.com/portfolio/example-3. So, everything is working fine, just we need to resolve the issue/bug in the current code. So when the user wants to see details from example.com/portfolio/example-2 to read all details that belong to that project. Please, read carefully the question, there is all information provided.

1 Answers

Finally, I found the solution to this issue. In page example.com/portfolio/post.php update code from:

$link = null;
if(!empty($_GET['id'])) {
    $link = $_REQUEST['id'];
}
if (null === $link) {
    header('index');
} else {
    $sql = "SELECT * FROM projects WHERE link = :link";
    $query = $dbh->prepare($sql);
    $query->bindParam(':link', $link, PDO::PARAM_INT);
    $query->execute();
    $project = $query->fetch(PDO::FETCH_ASSOC);
    $cnt = 1;
}

to:

$link = null;
if(!empty($_GET['id'])) {
    $link = $_REQUEST['id'];
}
if (null === $link) {
    header('index');
} else {
    $sql = "SELECT * FROM projects WHERE link = :link";
    $query = $dbh->prepare($sql);
    $query->bindParam(':link', $link, PDO::PARAM_INT);
    /**
     *
     * The array(':link' => $_GET['id']);
     *
    */
    $query->execute(array(':link' => $_GET['id']));
    $project = $query->fetch(PDO::FETCH_ASSOC);
    $cnt = 1;
}
Related