I have a public user page where it displays the most current post belonging to that specific user. Right now, my user page is displayed as site.com/user.php?id=1
and I want to display the url as something along the lines of site.com/user.php?username=username
I have tried changing the $_GET['id']
and $id = $_GET['id'];
in the top portion of my code in user.php
to $_GET['username']
and $username = $_GET['username'];
and using an additional $_GET
for the user id. However, it will only show me a blank page when I use site.com/user.php?user=username
since I donât think itâs retrieving the id for that user?
If I use print_r($id)
, it will print the user id, but none of the post content (a title, description, link, and an image) attributed to that user shows up. It will also display the username if I use print_r($username)
.
I can get it to work just fine if I have a link like site.com/user.php?id=1&username=username
though.
Thanks in advance to anyone who offers suggestions! I really appreciate it since Iâm still pretty new to PHP.
Here is the pertinent PHP/HTML on the first page where the link is clicked by the user to view their user page
<?php
session_start();
$msg = "";
include('../includes/db_connect.php');
if (!isset($_SESSION['user_id'])) {
header('Location: ../login.php');
exit();
}
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
?>
**** there is a bunch of plain html here I omitted ****
<?php echo '<a href="../user.php?id='.$user_id.'&username='.$username.'" style="margin-top:15px;">VIEW USER PAGE</a>'?>
Here is the all of PHP located in user.php where the above link goes to
<?php
include('includes/db_connect.php');
if(!isset($_GET['id'])){
header('Location: index.html');
exit();
}else{
$id = $_GET['id'];
}
$username = $_GET['username'];
?>
**** there is a bunch of plain html here I omitted ****
<?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
$imgsql = "SELECT img_name, img_path FROM images WHERE post_id = '$post_id' LIMIT 1";
$query1 = $db->query($imgsql);
if($query1->num_rows>0){
while($imgrow = $query1->fetch_object()){
echo "<img src='member/images/".$imgrow->img_name."' width='100%' height='auto' >";
echo "<br><br><br>";
}
}
?>
<?php endwhile?>
Here are all the database tables Iâm working with
user
CREATE TABLE `user` (
`user_id` int(11) NOT NULL,
`password` varchar(255) NOT NULL,
`username` varchar(255) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
post
CREATE TABLE `post` (
`post_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`link` text NOT NULL,
`description` text NOT NULL,
`start` date NOT NULL,
`end` date NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
images
CREATE TABLE `images` (
`image_id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`img_name` varchar(255) NOT NULL,
`img_path` varchar(255) NOT NULL,
`img_type` varchar(255) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;