Jump to content

insert into two tables, foreign key and primary key


xjermx

Recommended Posts

I am trying to implement an email style messaging feature into a PHP application.

 

I have a 'users' table with a primary key that auto increments, and my plan is to do two mail tables,

mail1 has the fields mail1_id, message_text, from_user, timestamp (mail1_id is the primary key and autoincrements, from_user is a foreign key that will relate back to the users table)

mail2 has the fields mail2_id, to_user, link_to_mail1 (mail2_id is primary key and autoincrements, link_to_mail1 is intended to provide relationship between the table records)

 

I'm a newbie, so I'm assuming this is a descent setup.  I've done it this way so that if a user sends a message to multiple other users, it will minimize repetition in the table, especially of the message_text field.

 

My question is, how do I insert data into this arrangement from a PHP application?  I'm familiar with basic INSERT, but obviously I need to know what mail1_id ends up being, in order to populate the link_to_mail1 field.

 

I could probably use an INSERT, QUERY, INSERT scheme to do it, but I can't help but feel like there is a better way of doing this.

 

Any suggestions?

 

Thanks!

 

(edit:  I'm assuming that this should be here in MySQL instead of in PHP?)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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