I currently have a user page where it will show the user's most recent post (this is a non-Wordpress site). To access the page, I have it setup as site.com/user.php?id=1
so that it will show any user's page if their ID is entered.
However, what I would like to accomplish is getting the page to display as site.com/user.php?username=namehere
so someone can type in the username instead of the user's ID to go to the user page. This page can be accessed by anyone, not just logged in users.
I have everything working perfectly with the site.com/user.php?id=1
method. When I try the site.com/user.php?username=namehere
method, I am able to access a blank page of user.php
, but none of the user-specific content will show. I believe this is because the id attributed to that username is not being called for correctly.
Hopefully this explanation of the issue makes sense.
Does anyone have any suggestions on how I can accomplish what I want to?
I have been looking over similar topics on this forum and on others for the past couple hours, but I haven't had any luck with fixing my particular issue.I'm still new to working with PHP, so I'm probably assuming that I am missing something simple.
I will show what I've tried and what I'm currently working with below this explanation of my issue.
Here is the top portion of my user.php for when I am able to successfully use site.com/user.php?id=1
without error:
<?php
include('../includes/db_connect.php');
if(isset($_GET['id'])){
$id=$_GET['id'];
}else{
}
?>
Here is the top portion of my user.php for when I am am trying to get the username for site.com/user.php?username=namehere
:
<?php
include('../includes/db_connect.php');
if(isset($_GET['id'])){
$id=$_GET['id'];
echo "ID YES";
}else{
echo "this is not getting an id";
}
if(isset($_GET['username'])){
$username = $_GET['username'];
echo "USERNAME YES";
}else{
echo "this is not getting a username";
}
?>
I included the $_GET['id']
as well in the hopes of being able to call for it so the content shows, but that returns that the page is not getting an id.
It shows the error Undefined index: id on line 47
. Line 47 comes from the code chunk below where I am calling for a user's post. That line singled out is: $queryString = "SELECT post_id, title, description, link FROM post WHERE user_id='$id' ORDER BY post_id DESC LIMIT 1";
Here is the rest of user.php from the HTML where it shows the page content if anyone wants to see that for any reason:
<?php
$queryString = "SELECT post_id, title, description, link FROM post WHERE user_id='$id' ORDER BY post_id DESC LIMIT 1";
$query = $db->prepare($queryString);
$query->execute();
$query->store_result();
$query->bind_result($post_id, $title, $description, $link);
while ($query->fetch()):
?>
<h2><?php echo $title?></h2>
<a href="<?php echo $link?>"><?php echo $link?></a>
<div class="description"><?php echo $description?></div>
<?php endwhile?>
Then if I try site.com/user.php?id=1&username=namehere
with both the $_GET['id']
and $_GET['username']
in the top portion of PHP, the page displays the correct content without error. The id and username are both being returned as being there.