Jump to content

A little help linking


takn25

Recommended Posts

Hi, I am facing a slight problem I have created a comment box, where users can post while logged in. The thing I can not figured out is how to link, user details from the user table with the comments table. The furthest I have gotten is posting, all user details in the comments table but this is not useful as once the user details are posted, in the comments table they wont update, if the user changes his first name for instance. So please could some one guide me a bit what can I do so this all works. Thanks in advance I hope I was clear enough on what I am after. 

Link to comment
https://forums.phpfreaks.com/topic/224931-a-little-help-linking/
Share on other sites

Have one table that is your user table, which should have a primary key (auto-increment most likely).  In the comments table, don't put the user's name, instead put the ID from the user table.  Then, when you list the comments, you can perform a join to get the information from the user table, including the current name (and avitar and whatever else you might need).  So your tables might be:

 

user
------
user_id (primary autoincrement)
user_name

comment
------
comment_id (primary autoincrement)
user_id (foreign key referencing the user table)
comment_text

 

to get the username and comment list:

 

select user_name, comment_text from comment join user using (user_id)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.