MySQL insert into many-to-many junction table when the IDs are auto_increment
<p>I'm trying to wrap my head around many-to-many table connections.
I have three tables:  <code>Modules</code>, <code>Phases</code>, and <code>Module_Phases</code> (junction).</p>

<p>Both <code>Modules</code> and <code>Phases</code> have primary keys as auto_increment.</p>

<pre><code>create table Modules
(
    MID int not null auto_increment primary key,
    modulesID int unique,
    title varchar(20),
    article Text,
    dateAdded datetime default null,
    UID int,
    pictureurl varchar(100),
    linkurl varchar(50),
    KID int
);
</code></pre>



<pre><code>create table Phases
(
    PID int not null auto_increment primary key,
    phasesID int(1),
    phasename varchar(30),
    phaseage varchar(10),
    phasearea varchar (30),
    MID int
);
</code></pre>



<pre><code>create table Phases_Modules
(
    PID int not null,
    MID int not null
);
</code></pre>

<p>When I insert into a table, I usually disregard the primary key and just use:</p>

<pre><code>insert into Modules(modulesID,title,article,pictureurl,linkurl) values (1,"namex","textcontent","text2","text3");
</code></pre>

<p>On the web, I've seen people suggest using:</p>

<pre><code>insert into Phases_Modules(PID,MID) values (x,x);
</code></pre>

<p>But since both of the tables have auto_increment on the primary key, how do I go about using the correct ID key?  I've seen some examples where they use <code>LAST_INSERT_ID</code>.  But can I, for example, use something like this:</p>

<pre><code>insert into Phases_Modules(PID,MID) values (PID where Phases.phaseID=x, Modules.Last_Insert_ID);
</code></pre>

<p>I know this was kind of a bogus line, but it was just to try to explain the issue. :)</p>