user3371397
user3371397

Reputation: 55

Session to echo a row retrieved from database

I have a system where users can send and receive messages and I need to make a way for them to directly reply to the messages.

This is a test code to make the page echo what should be retrieved and set as a session, but it is not echoing anything.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Clan Kaos ● View Post</title>
  <link rel="stylesheet" type="text/css" href="include/style/content.css" />
</head>
<body>

<?php

include ("include/header.html");

include ("include/sidebar.html");

include ("include/testpost.html");
?>
<div class="container">

<?php   session_start();
  require_once('appvars.php');
  require_once('connectvars.php');

  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

      $query = "SELECT username FROM kaoscraft_user WHERE user_id = '" . $_SESSION['user_id'] . "'";
        $data = mysqli_query($dbc, $query);
            $row = mysqli_fetch_array($data);
            $username1 = $row['username'];

  // Grab the profile data from the database
  $query = "SELECT `to`, `from`, rank, gender, picture, title, msg FROM kaoscraft_pm WHERE `to` = '$username1' ORDER BY msg_id DESC";
  $data = mysqli_query($dbc, $query);
  $gender = $row['gender'];
    $username2 = $_SESSION['reply'];


  while ($row = mysqli_fetch_array($data)) {
    $_SESSION['reply'] = $row['from'];
  echo '<div class="viewpost">';
  echo '<div class="vpside">';
    if(!empty($row['picture'])) {
    echo '<img class="pictest" src="' . MM_UPLOADPATH . $row['picture'] . '" alt="' . MM_UPLOADPATH . 'nopic.png' . '" />';
    }
  if(!empty($row['from'])) {
      echo '<p>From:<br />' . $row['from'] . '</p>';
        echo '<p> ' . $username2 . '</p>';
    }
  if(!empty($row['rank'])) {
      echo '<p>Rank:<br />' . $row['rank'] . '</p>';
    }
  if(!empty($row['gender'])){
    echo '<p>Gender:<br /> ' . $row['gender'] . '</p>';
  }  
  echo '</div>';

    if(!empty($row['title'])) {
        echo'<h4><u>' .$row['title']. '</u></h4>';
    }
      if(!empty($row['msg'])) {
      echo '<p class="">' . $row['msg'] . '</p>';
    }
        echo '<div class="sig">';
      if(!empty($row['bio'])) {
      echo '<p>' . $row['bio'] . '</p>';
      }
      echo '</div>';
    echo '</div><br />'; 
    }
  mysqli_close($dbc); 
?>

I need the session $_SESSION['reply'] to be set as the variable $username2 and it does not seem to be working.

Upvotes: 1

Views: 465

Answers (2)

user3371397
user3371397

Reputation: 55

What I was doing was incorrect. I had to move

$_SESSION['reply'] = $row['from'];
$username2 = $_SESSION['reply'];`

from after the query to

while ($row = mysqli_fetch_array($data)) {
    $_SESSION['reply'] = $row['from'];
    $username2 = $_SESSION['reply'];`

and then it echo'd correctly. Thanks for the help from everyone :)

Upvotes: 1

Move session_start(); on top of the PHP code. You are accessing $_SESSION['reply'] = $username2; before that which wont work.

Upvotes: 2

Related Questions