title: Using MySQL and a PHP form to insert data into a relational database
body: <p>I have a MySQL database with 3 tables: articles, tags, and articles_tags</p>
  
  <p>The schema for each follows:</p>
  
  <p><strong>articles:</strong>
  id - articletitle - articleorganization - articledate - articleurl </p>
  
  <p><strong>tags:</strong>
  id - tag_contents</p>
  
  <p><strong>articles_tags:</strong>
  id - article_id - tag_id</p>
  
  <p>I also have 2 PHP forms: addnew.php and edit.php</p>
  
  <p>This question specifically concerns addnew.php but it also related to edit.php.</p>
  
  <p>The form in addnew.php does what its name implies and creates new entries into the database. Currently it makes use of some text entry fields and 2 checkboxes. So, for example, I can insert the Title, Author, URL, and Access Date of the article into the <strong>articles</strong> table while also inserting the tags (by checking either or both of the checkboxes) into the <strong>tags</strong> table. What I'm not able to figure out is how do I make these tables intersect with the third table? I've looked around online but I can't seem to find what I need.</p>
  
  <p>For reference the code for addnew.php is below:</p>
  
  <pre><code>    &lt;?php
       function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
       {
       ?&gt;
      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
      &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
      &lt;head&gt;
      . . .
      &lt;/head&gt;
  
      &lt;body&gt;
  
      &lt;div class="container"&gt;
        &lt;div class="header"&gt;
        . . .
        &lt;/div&gt;
          &lt;div class="sidebar1"&gt;
          . . .
          &lt;/div&gt;
        &lt;div class="content"&gt;
              &lt;div id="stylized" class="myform"&gt;
            &lt;form id="form" name="form" action="" method="post"&gt;
              &lt;h1&gt;Create a new entry in the database&lt;/h1&gt;
              &lt;table width="100%" border="0" cellpadding="6"&gt;
                &lt;tr&gt;
                  &lt;td colspan="2"&gt;&lt;legend&gt;Article details&lt;/legend&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td width="20%" align="right"&gt;&lt;span class="field"&gt;Article Title:&lt;/span&gt;&lt;/td&gt;
                  &lt;td width="80%" align="left"&gt;&lt;span class="field"&gt;
                    &lt;input name="articletitle" type="text" value="&lt;?php echo $articletitle; ?&gt;" size="50"/&gt;
                  &lt;/span&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td align="right"&gt;&lt;span class="field"&gt;Article Author:&lt;/span&gt;&lt;/td&gt;
                  &lt;td align="left"&gt;&lt;span class="field"&gt;
                    &lt;input name="articleorganization" type="text" value="&lt;?php echo $articleorganization; ?&gt;" size="50"/&gt;
                  &lt;/span&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td align="right"&gt;&lt;span class="field"&gt;Access Date:&lt;/span&gt;&lt;/td&gt;
                  &lt;td align="left"&gt;&lt;span class="field"&gt;
                    &lt;input name="articledate" type="text" value="MM/DD/YYYY" size="50"/&gt;
                  &lt;/span&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td align="right"&gt;&lt;span class="field"&gt;Article URL:&lt;/span&gt;&lt;/td&gt;
                  &lt;td align="left"&gt;&lt;span class="field"&gt;
                  &lt;input name="articleurl" type="text" value="&lt;?php echo $articleurl; ?&gt;" size="50"/&gt;
                  &lt;/span&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td align="right"&gt;&lt;span class="field"&gt;Article Tags:&lt;/span&gt;&lt;/td&gt;
                  &lt;td align="left"&gt;&lt;span class="field"&gt;
                          &lt;input type="checkbox" name="articletags[]" value="geology" id="articletags_0" /&gt;
                          &lt;input type="checkbox" name="articletags[]" value="astronomy" id="articletags_1" /&gt;
                  &lt;/span&gt;&lt;/td&gt;
                &lt;/tr&gt;
              &lt;/table&gt;
              &lt;footer&gt;&lt;input type="submit" name="submit" value="Add this Article"&gt;&lt;/footer&gt;
              &lt;/form&gt;
          &lt;/div&gt;
        &lt;div class="footer"&gt;
         . . .
         &lt;/div&gt;
      &lt;/body&gt;
      &lt;/html&gt;
      &lt;?php 
       }
  
       include('settings.php');
  
       if(count($articletags) &gt; 0)
      {
       $articletags_string = implode(",", $articletags);
      }
  
       if($_SERVER['REQUEST_METHOD'] == 'POST')
       { 
  
       $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
       $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
       $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
       $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
       $articletags = implode(',', $_POST['articletags']);
  
  
       if ($articletitle == '' || $articleorganization == '')
       {
  
       $error = 'ERROR: Please fill in all required fields!';
  
  
       renderForm($articletitle, $articleorganization);
       }
        else
       {
  
       mysql_query("INSERT INTO articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl' ");
       mysql_query("INSERT INTO articles_tags SET articletags='$articletags' ")
  
  
       or die(mysql_error()); 
  
  
       header("Location:addsuccess.php"); 
       }
       }
       else
  
       {
        renderForm('','','','','');
       }
      ?&gt;
  </code></pre>