954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP MySQL problem

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?

jeffro25
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Well, I am trying to use the login name as the post name for the message. I can set the $_session['MM_Username'] 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.

jeffro25
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

jeffro25
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 
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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
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!

jeffro25
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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

jeffro25
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 
Still having problems with this...any ideas anyone?


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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

all-stores-bitmap.jpg     Log out Welcome to Bruces Riders!   <?php echo $_SESSION['MM_Username']; ?> ,please feel free to post a message.
Type message here.





 

jeffro25
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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['userId']. 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?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

jeffro25
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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

$userId = $_SESSION['userId'];
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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...

jeffro25
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You