Showing posts only created by user when given user is logged in

i’ve created a basic crud website where the user can register then create a post that displays on the home page along with other registered users posts. Where I am stuck is when the user is logged in I wanted them to be able to view only the posts they created in their profile section.
So far i’m at the point where i’ve created a user_id column in the posts table under phpmyadmin and if I manually add the user_id in the users post of the given user the post does appear in their profile.

I’m stuck trying to figure out how to have the currently logged in users id added to the post they created. My plan was to try pulling it from the $_SESSION.
This is the page I would plan to have the user_id added to the post they create

<!-- add to header file used for redirecting pieces of code. buffers request in headers of script course 104-->
<?php ob_start(); ?>
<?php include "header.php"; ?>

<div class="container">
  <h1>Welcome</h1><h2><?php echo $_SESSION['username']; ?></h2>

<a href='../index.php'>Back</a>

  <h1>Create Post</h1>

  if(isset($_POST['create_post'])) {

    $post_title = $_POST['title'];
    $post_author = $_POST['author'];
    $post_category_id = $_POST['post_category'];
    $post_status = $_POST['post_status'];

    $post_image = $_FILES['image']['name'];
    $post_image_temp = $_FILES['image']['tmp_name'];

    $post_tags = $_POST['post_tags'];
    $post_content = $_POST['post_content'];
    $post_date = date('d-m-y');

    move_uploaded_file($post_image_temp, "../../images/$post_image");

    $query = "INSERT INTO posts(post_category_id,post_title,post_author,
      post_date,post_image,post_content,post_tags,post_status ) ";

    $query .= "VALUES('{$post_category_id}','{$post_title}','{$post_author}',now(),
    '{$post_image}', '{$post_content}', '{$post_tags}', '{$post_status}' ) ";

      $create_post_query = mysqli_query($connection, $query);

      if(!$create_post_query ) {

        die("QUERY FAILED ." . mysqli_error($connection));



  <form action="" method="post" enctype="multipart/form-data">

    <label for="title">Post Title</label>
    <input type="text" class="form-control" name="title">
    <label for="category">Category</label>
    <select name="post_category" id="post_category">

      $query = "SELECT * FROM categories ";
      $select_categories = mysqli_query($connection, $query);

      while($row = mysqli_fetch_assoc($select_categories)) {
      $cat_id = $row['cat_id'];
      $cat_title = $row['cat_title'];

        echo "<option value='{$cat_id}'>{$cat_title}</option>";


    <label for="title">Post Author</label>
    <input type="text" class="form-control" name="author">
    <label for="post_status">Post Status</label>
    <input type="text" class="form-control" name="post_status">
    <label for="post_image">Post Image</label>
    <input type="file" name="image">
    <label for="post_tags">Post Tags</label>
    <input type="text" class="form-control" name="post_tags">
    <label for="post_content">Post Content</label>
    <textarea class="form-control" name="post_content" id="" cols="30" rows="10">
    <input class="btn btn-primary" type="submit" name="create_post" value="Publish Post">

    <?php include "footer.php"; ?>

Well, first, replace the object start with a session start!
This will start the session for you. Next, in your login code, set the username. Normally, you would blank out the username when someone first comes to your site. That way, the rest of your site will know that the user has not logged in. You can set it like this:
$_SESSION[‘username’] = “”; OR unset($_SESSION[‘username’]);
Once then log in, set it to their username. And, then, at the top of each page where different test sections need to be displayed or not displayed, you can test for it like this:
if ( isset( $_SESSION[‘username’] ) ) { do something… }
if ( $_SESSION[ ‘username’ ] != “” ) { do something… }
This depends on how you set or unset the session variable. Often, you would put the login code in your header file along with starting the session. The first thing that code would do is check if the username was stored and if not, show a login button. If set, then it would say welcome-back-username or something like that.
Hope this helps get you started… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service