Hey guys. Got a quick question. I am having a problem with a message board that I am creating. I have created the log in and registration page and the sessions needed to use the site nad it all works fine, but when the user posts a message I can't get the unique user name to appear for the message that user posts. I can get it to appear as long as the users session is still valid, but once the user logs out, the session is terminated and the users name is gone from the message board. Please help?

Are you not storing the user id with the post? If you are then you just need to display the name associated with that id. The name (id) associated with a post should not have any dependency on whether a user is logged in.

Well, I am trying to use the login name as the post name for the message. I can set the $_session to it and it works until they log out. I can make it show up, but the user would have to put their user name in again with the message and thats what I am trying to avoid.

If the login name is the id for the user in the database, then that's fine to keep it in the session while they are logged in. You will still need to write that value to the database with the post though. The page that displays posts should retrieve the name of the poster along with the post from the database.

Thats where I think I am giong wrong. I've tried to do that, but it just doesn't work. Any suggestions on the line of code that i could use? I've tried setting the $loginuser name and session variables to the user id in the database. Obviously not the right thing to do.

I've tried setting the $loginuser name and session variables to the user id in the database. Obviously not the right thing to do.

Actually, I would store both the user name and the id in the session. Whatever statement that saves the post just needs to include the id. The query that shows posts just needs to display the name along side the comment, so pull all that info in a single query. If you are confused by that, post your code and we can figure out how to change it.

Actually, I would store both the user name and the id in the session. Whatever statement that saves the post just needs to include the id. The query that shows posts just needs to display the name along side the comment, so pull all that info in a single query. If you are confused by that, post your code and we can figure out how to change it.

Aw man....I get it...Thanks...I am about to head out of work for the day...We'll see how it goes tonight and I'll let you know tomorrow...Thanks for all your help!

Still having problems with this...any ideas anyone?

Still having problems with this...any ideas anyone?

Post the relevant code. Without that we can only speculate and offer generic suggestions.

<?php require_once('Connections/riders.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}



$logoutAction = $_SERVER."?doLogout=true";
if ((isset($_SERVER)) && ($_SERVER != "")){
$logoutAction .="&". htmlentities($_SERVER);
}


if ((isset($_GET)) &&($_GET=="true")){
$_SESSION = NULL;
$_SESSION = NULL;
$_SESSION = NULL;
unset($_SESSION);
unset($_SESSION);
unset($_SESSION);


$logoutGoTo = "index.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;


$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);


switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}


$editFormAction = $_SERVER;
if (isset($_SERVER)) {
$editFormAction .= "?" . htmlentities($_SERVER);
}


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO messages (message) VALUES (%s)",
GetSQLValueString($_POST, "text"));


mysql_select_db($database_riders, $riders);
$Result1 = mysql_query($insertSQL, $riders) or die(mysql_error());


$insertGoTo = "messagepost.php";
if (isset($_SERVER)) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER;
}
header(sprintf("Location: %s", $insertGoTo));
}
?><!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bruces Rider's Social Network. Plan rides, meet new people, or just bike talk.</title>
<style type="text/css">
<!--
body {
background-color: #000000;
}
.style9 {
font-size: 48px;
color: #FFFFFF;
}
.mystyle {
font-family: Arial, Helvetica, sans-serif;
color: #FFFFFF;
font-size: medium;
font-weight: bold;
}
.style11 {color: #FFFFFF}
-->
</style></head>


<body>
<table width="834" height="477" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" rowspan="2" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" height="192" valign="top"><img src="images/all-stores-bitmap.jpg" width="241" height="192" /></td>
</tr>
<tr>
<td height="92" valign="top"><table width="100%" height="105" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" height="22" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
</tr>
<tr>
<td height="49">&nbsp;</td>
</tr>
<tr>
<td height="34" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" height="34" valign="top" class="mystyle"><a href="<?php echo $logoutAction ?>" class="style11">Log out</a></td>
</tr>
</table>            </td>
</tr>
</table>        </td>
</tr>
</table></td>
<td width="593" height="57" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="589" height="49" valign="top"><div align="center"><span class="style9">Welcome to Bruces Riders!</span></div></td>
</tr>
</table>
<td width="593" height="156">&nbsp;</td>
</tr>
<tr>
<td width="593" height="113" valign="top" class="mystyle"><?php echo $_SESSION; ?> ,please feel free to post a message.
<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
<br />
<label>
<textarea name="textarea" id="textarea" cols="45" rows="5">Type message here.</textarea>
</label>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</p>



<input type="hidden" name="MM_insert" value="form1" />
</form>              </td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="19" colspan="2">&nbsp;</td>
</tr>
</table>
</body>
</html>

This is the page that actually inserts the record into the database.

Quick aside: Place code tags around code to maintain formatting. It really helps readability.

The part you'll need to change is

$insertSQL = sprintf("INSERT INTO messages (message) VALUES (%s)",
                       GetSQLValueString($_POST['textarea'], "text"));

I'm not certain how that table is structured, but you will need to add the user id something like this

$insertSQL = sprintf("INSERT INTO messages (userId, message) VALUES (%d,%s)", $userId, GetSQLValueString($_POST['textarea'], "text"));

$userId could be held in the session along with your other properties $_SESSION. You'll also need to make sure that the query which display the messages returns the user name along with the message and use that instead of the logged in user name from the session.

Does that clear it up a bit?

That worked to a point...i understand what you did, but I don't understand why it's returning a value of 0 (zero) for the userid. Everyone that I insert into the database is giving me the same userid of 0

You will need to store the user id in the session when they log in and retrieve it as needed

$userId = $_SESSION['userId'];

Now it won't let me enter the user name into the database because it's already there...I'm confued. Keeps telling me 'duplicate entry for key 1'...I double checked the database and it's not set to unique or primary...I now have no idea...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.