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).
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:
Article Title, Article Organization, Access Date, Article URL, and Article Tag(s) (and there is also an id column for this table.
The title, organization, date, and url are all stored in a table named articles.
The tags are all stored in a table named tags (there are 85 of them). This table has 2 columns: id and tag_contents.
The 3rd table is a relation table (or maybe it’s called an “intersection table”?) called articles_tags with 2 columns: article_id and tag_id.
What new.php currently does is add the title, organization, date, and url to the articles table and, then, uses mysql_insert_id() to get the ID of that entry.
But, after that, I can’t figure out what I need to do with my checkboxes.
I have 3 checkboxes:
<input type="checkbox" name="articletags[]" value="science" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="geology" id="articletags_1" />
<input type="checkbox" name="articletags[]" value="astronomy" id="articletags_2" />
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:
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)
And, the database tables would look like this:
(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
Note that the tags table doesn’t change, it only stores the available tags and allows them to be referenced by the table articles_tags.
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:
<?php
}
// connect to the database
include('settings.php');
if(count($articletags) > 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('','','','','');
}
?>
then in php