Getting username to display variable

I am using $_SESSION[username] to connect to another table and to display and add the variable to the table with no luck. How can I set this up. I know there is a way but I am not getting it. post_author and post_author_id are suppose to go into the db when I hit the submit button but only the post_author_id is responding.

I have the errors ALL set up and it keeps letting me know the variable is undefined. When I put the $_SESSION[username] as the $_SESSION[SESS_ID] it displayed the id of the member. I have put the variable at the beginning as $username - ‘’; right now the line that is showing as error is


$post_author = preg_replace('#[^0-9]#i', '', $_SESSION['username']);

this is part of the file that is suppose to activate the info add to the db and display the username onto the page.


if (!isset($_SESSION['SESS_ID']) || (trim($_SESSION['SESS_ID']) == '') || isset($_SESSION['username'])) { 
   '<a href="http://www.blessedtalk.com">Register Account</a>
	    |    
	 <a href="http://www.blessedtalk.com/login-form.php">Log In</a>';
   }

// Check the HTTP_REFERER for light level security
$ref = parse_url($_SERVER['HTTP_REFERER']); 
$host = $ref["host"];
if ($host != "www.blessedtalk.com") {
	echo "Please log in at the home page.";
	exit();
}

$id = "";
$username = "";
$firstname = "";
$lastname = "";
$post_author = "";	
	// ------- ESTABLISH THE PAGE ID ACCORDING TO CONDITIONS ---------

if (isset($_GET['id'])) {
	 $id = $_GET['id']; // filter everything but numbers
} else if (isset($_SESSION['SESS_ID'])) {
	 $id = $_SESSION['SESS_ID'];
} else {
   '<a href="http://www.blessedtalk.com/login-form.php">Log In</a>';
   
}
// ------- END ESTABLISH THE PAGE ID ACCORDING TO CONDITIONS ---------
// ------- FILTER THE ID AND QUERY THE DATABASE --------
$sql = mysql_query("SELECT username, id FROM `myMembers` WHERE id='".$_SESSION["SESS_ID"]."'"); // query the member
// ------- FILTER THE ID AND QUERY THE DATABASE --------

// ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount < 1 ) { // evaluate the count
	 header("location: index.php?msg=user_does_not_exist");
     exit();
}
while($row = mysql_fetch_array($sql)){ 
    $username = $row["username"];
	$post_author = $row["username"];
	$post_author_id = $row["id"];
}

// Be sure all form variables are present to proceed
if (!isset($_POST['post_type']) || !isset($_POST['post_body']) || !isset($_POST['fsID']) || !isset($_POST['fsTitle']) || !isset($_POST['uid']) || !isset($_POST['upass'])) {
	echo "Important variables from the form are missing.";
	exit();
}
// Filter all of the common variables
$post_type = $_POST['post_type']; 
$post_body = $_POST['post_body'];
$post_body = nl2br(htmlspecialchars($post_body));
$post_body = mysql_real_escape_string($post_body);
$forum_section_id = preg_replace('#[^0-9]#i', '', $_POST['fsID']); 
$forum_section_title = preg_replace('#[^A-Za-z 0-9]#i', '', $_POST['fsTitle']); 
$member_id = preg_replace('#[^0-9]#i', '', $_POST['uid']); 
$post_author = preg_replace('#[^0-9]#i', '', $_SESSION['username']); 
$member_password = mysql_real_escape_string($_POST['upass']);

$sql = mysql_query("SELECT * FROM forum_sections WHERE id='$forum_section_id' AND title='$forum_section_title'");
$numRows = mysql_num_rows($sql);
if ($numRows < 1) {
	    echo "ERROR: That forum section does not exist.";
	    exit();
}
// Prevent this member from posting more than 30 times in one day
$sql = mysql_query("SELECT id FROM forum_posts WHERE post_author_id='$member_id' AND DATE(date_time) = DATE(NOW()) LIMIT 32");
$numRows = mysql_num_rows($sql);
if ($numRows > 30) {
	echo "ERROR: You can post only 30 times per day. Your maximum has been reached.";
    exit();
}
if ($post_type == "a") {
	$post_title = preg_replace('#[^A-za-z0-9 ?!.,]#i', '', $_POST['post_title']);	
	if ($post_title == "") { echo "The Topic Title is missing."; exit(); }
	if (strlen($post_title) < 10) { echo "Your Topic Title is less than 10 characters."; exit(); }
	$sql = mysql_query("INSERT INTO forum_posts (post_author, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) 
     VALUES('$post_author','$member_id',now(),'a','$forum_section_title','$forum_section_id','$post_title','$post_body')") or die (mysql_error());
	$this_id = mysql_insert_id();
	//$sql = mysql_query("UPDATE forum_posts SET otid='$this_id' WHERE id='$this_id'"); 
	header("location: view_thread.php?id=$this_id"); 
    exit();
}

I’m not sure on the cause but I have had a similar problem in the past. I THINK that when you set $username to $row[‘username’] that unsets it from $row.

Try this in your while loop

$username = $row[‘username’];
$post_author = $username;

I would have never figured that out. Thanks it worked like a charm.

My first time successfully helping someone, yay!

Also, if you only have one record/user, you probably don’t need the while loop part.