Username in url instead of user id PHP

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;

I was wanting to help but you’ve got too much code to go through. Could you please try to really identify where you think your problem is and ask your question more specifically and better targeted (leaving out anything extraneous)? Thanks.

Most common ways to pass array parameters to a PHP web page are GET, POST and SESSION. I usually create a debug function called Fred(…) because it is easier to type than debug and display the array variables at the top of the page:

<?php

fred ( $_SESSION );

fred ( $_POST );

fred ( $_GET );
// Lots of other script here

//==============
function ( $val )
{
  echo '<hr>';
  echo '<pre>';
    print_r( $val );
  echo '</pre> <br>';
}

// end if file and do not display closing PHP

Post array is passed from a form with mode set to ‘post’ or ‘get’.

The Get array is parsed from the browser command line.

Session array item variables are common to all PHP pages and requure session_start(); to be called only once before setting a session array item. Calling session_start(); a second time will invoke an error or warning.

That’s only because you’re selecting the user_id for your post table. You need grab the username from the URL, then use that username’s data and select just the user_id. Next, use that user_id to select only posts that contain that user_id. This might require more than 1 query.

You are using prepared statements, please use them properly.

Correct. This bit of code here:

if(!isset($_GET['id'])){
  header('Location: index.html');
  exit();
}else{
  $id = $_GET['id'];
}

looks to see if you specify the id parameter in the URL, and if you do not, then it redirects you to the index page. So at the very least, if you want to just use the username, you need to lose that check, or alter it so it looks for the username instead of the id.

Then it’s just as @spaceshiptrooper said, your query relies on selecting posts by id, not by username, so you need to alter that as well - if you cut out the id parameter from your URL, then there is no $id value to put in the query.

As you said you’re new to PHP (and could we presume MySQL as well?) initially it might be easiest to run another query first, just to get the user_id for the supplied username, and then stick to your initial query to get the posts. Once that’s working, you could alter it to use a single query with a JOIN parameter, and select the posts using the username.

On the database, you might want to add a “unique” flag to the username column in your users table, though your user creation code might enforce this.

I must say I probably wouldn’t pass the username around to keep user details, I’d use the id instead. Apart from anything else, you have extra work to do if your username could contain any characters that are not URL-friendly, such as spaces and the like.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.