views:

87

answers:

2

I have two tables - a 'users' and a 'comments'. They both have the coloum 'IDUsers'. When a user adds a comment I want their user ID to come from the 'users' table and go into the 'comments' table into the 'IDUsers' coloum from the 'IDUsers' coloum.

However, at the same time a comment is being added from the user - so I also am using the INSERT INTO for the new information. I'm also using ColdFusion - if that makes much difference.

Hope everyone understands what I'm saying....and thanks for the help.

+2  A: 

What exactly is the problem? When a user is posting a comment, you know their user ID. So use that UserID to do the insert into the comments table.

If you're generating the user object on the fly - as a part of the same transaction (make sure it's a single transaction! It's important - synchronization issues!!!) - use something like mysql_insert_id() equivalent in ColdFusion (http://www.coldfusionmuse.com/index.cfm/2005/8/8/identity%20after%20insert). After you do the insert into the user table, get the ID of the inserted record; then use that ID as the foreign key into the comments record.

Alex
Hey Alex, thanks for your reply. When I enter in that code it still doesn't work. If I put a 'hidden feild' in the form with the value of the IDUser in it from the 'users' table - when the users hit submit will ti then go into the IDUsers coloum in the 'comments' table? To do this I'll have to do the same thing as with a persionalised welcome message (?). To do this would I just create a recordset and drop the dynamic tags onto the page e.g. <cfoutput>#Recordset1.Username#</cfoutput>? - yet doesn't work! It only uses the first users username no matther which user is loged on.
Bridget
A: 

You will know the userId while a user is adding comments. Use that particular userId in the insert statement. And to make sure that only userId from users table comes in comments table, you can set userId in comments table as a foreign key from users table.

Richie