title: Using checkboxes in a PHP form with MySQL and multiple tables
body: <p>I am trying to use a PHP form (new.php) to update 2 MySQL tables and retrieve information from a 3rd. The reason I need to use 3 tables is so that the information stored can be searched via another form (search.php) and, also, so that the information can be easily edited (edit.php).</p>
  
  <p>New.php makes use of 4 text entry fields and, currently, 3 checkboxes (though this number will increase once I've gotten these 3 to work). The checkboxes act as "tags" that are used to describe articles that are entered into the database. So, a single entry in the database contains the following information:</p>
  
  <p>Article Title, Article Organization, Access Date, Article URL, and Article Tag(s) (and there is also an <code>id</code> column for this table.</p>
  
  <p>The title, organization, date, and url are all stored in a table named <code>articles</code>.</p>
  
  <p>The tags are all stored in a table named <code>tags</code> (there are 85 of them). This table has 2 columns: <code>id</code> and <code>tag_contents</code>.</p>
  
  <p>The 3rd table is a relation table (or maybe it's called an "intersection table"?) called <code>articles_tags</code> with 2 columns: article_id and tag_id.</p>
  
  <p>What new.php currently does is add the title, organization, date, and url to the <code>articles</code> table and, then, uses <code>mysql_insert_id()</code> to get the ID of that entry.</p>
  
  <p>But, after that, I can't figure out what I need to do with my checkboxes.</p>
  
  <p>I have 3 checkboxes:</p>
  
  <pre><code>&lt;input type="checkbox" name="articletags[]" value="science" id="articletags_0" /&gt;
  &lt;input type="checkbox" name="articletags[]" value="geology" id="articletags_1" /&gt;
  &lt;input type="checkbox" name="articletags[]" value="astronomy" id="articletags_2" /&gt;
  </code></pre>
  
  <p>I need to somehow use those checkboxes to make a relation with the article. So, for example, after inserting an article about rocks the form would look like:</p>
  
  <pre><code>Article Title: Great new rocks!
  Article Organization: US Geology Assoc.
  Access Date: 09/20/2012
  Article URL: www.rocks.com
  
  Science [X]    Geology [X]    Astronomy [ ] (note that the X marks a checked checkbox)
  </code></pre>
  
  <p>And, the database tables would look like this:</p>
  
  <pre><code>(table: articles) id | articletitle     | articleorganization | articledate | articleurl
                    13 | great new rocks! | US geology assoc.   | 9/20/2012   | www.rocks.com
  
  
  (table: tags)     id | tag_contents
                     5 | geology
                     9 | science
  
  (table: articles_tags)   article_id  | tag_id
                              13       |   5
                              13       |   9
  </code></pre>
  
  <p>Note that the <code>tags</code> table doesn't change, it only stores the available tags and allows them to be referenced by the table <code>articles_tags</code>.</p>
  
  <p>I can not figure this situation out and I've been hacking at it for a week now. For reference, the code for new.php is added below:</p>
  
  <pre><code>    &lt;?php 
   }
  
   // connect to the database
   include('settings.php');
  
   if(count($articletags) &gt; 0)
  {
   $articletags_string = implode(",", $articletags);
  }
   // check if the form has been submitted. If it has, start to process the form and save it to the database
   if($_SERVER['REQUEST_METHOD'] == 'POST')
   { 
   // get form data, making sure it is valid
   $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']));
  
   // check to make sure both fields are entered
   if ($articletitle == '' || $articleorganization == '')
   {
   // generate error message
   $error = 'ERROR: Please fill in all required fields!';
  
   // if either field is blank, display the form again
   renderForm($articletitle, $articleorganization);
   }
   else
   {
   // save the data to the database
   mysql_query("INSERT INTO articles SET articletitle='$articletitle',
        articleorganization='$articleorganization',
        articledate='$articledate',
        articleurl='$articleurl' ")
  
   or die(mysql_error()); 
  
   $article_id = mysql_insert_id();
  
   foreach( $POST_['articletags'] as $newtag )
  {
     mysql_query('INSERT INTO articles_tags (article_id,tag_id) VALUES ($article_id, $newtag)');
  }
  
   // once saved, redirect to success page
   header("Location:addsuccess.php");  
   }
   }
   else
   // if the form hasn't been submitted, display the form
   {
   renderForm('','','','','');
   }
  ?&gt;
  </code></pre>