My goal is to create an entry form (addnew.php) that will allow me to later edit the entries made by using a different form (edit.php).
Currently addnew.php makes use of a few text fields and 2 checkboxes and what I’m attempting to do is to insert the information from the forms into a MySQL database. The information from the text fields is inserted into one table and the information from the checkboxes is inserted into a different table.
Now, though, I’m trying to use a 3rd table (if necessary) to make an intersection table that will allow me to use the same format used in addnew.php in the edit form.
So, in other words, addnew.php will have some text fields and check boxes and so will edit.php, the difference being that edit.php will have the information filled in and, in the case of the check boxes, they will be checked and the user will have the option to check more boxes or to uncheck already checked boxes.
The three tables that I have right now are:
articles:
id - articletitle - articleorganization - articledate - articleurl
tags:
id - tag_contents
articles_tags:
id - article_id - tag_id
And the code for addnew.php is:
<?php
function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
{
?>
. . .
<td width="20%" align="right"><span class="field">Article Title:</span></td>
<td width="80%" align="left"><span class="field">
<input name="articletitle" type="text" value="<?php echo $articletitle; ?>" size="50"/>
</span></td>
</tr>
<tr>
<td align="right"><span class="field">Article Author:</span></td>
<td align="left"><span class="field">
<input name="articleorganization" type="text" value="<?php echo $articleorganization; ?>" size="50"/>
</span></td>
</tr>
<tr>
<td align="right"><span class="field">Access Date:</span></td>
<td align="left"><span class="field">
<input name="articledate" type="text" value="MM/DD/YYYY" size="50"/>
</span></td>
</tr>
<tr>
<td align="right"><span class="field">Article URL:</span></td>
<td align="left"><span class="field">
<input name="articleurl" type="text" value="<?php echo $articleurl; ?>" size="50"/>
</span></td>
</tr>
<tr>
<td align="right"><span class="field">Article Tags:</span></td>
<td align="left"><span class="field">
<input type="checkbox" name="articletags[]" value="geology" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="astronomy" id="articletags_1" />
</span>
</td>
</tr>
<tr>
<td colspan="2" align="center" valign="middle"><input type="submit" name="submit" value="Add this Article" /></td>
</tr>
</table>
. . .
</html>
<?php
}
include('settings.php');
if(count($articletags) > 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' ")
$article_id = mysql_insert_id();
mysql_query("INSERT INTO tags SET articletags='$articletags' ");
$tag_id = mysql_insert_id();
mysql_query("INSERT INTO articles_tags SET article_id='$article_id',
tag_id='$tag_id' ")
or die(mysql_error());
header("Location:addsuccess.php");
}
}
else
{
renderForm('','','','','');
}
?>
What I’m now having trouble with is figuring out exactly what to do next (after the 2nd INSERT). I know that I need to set up a relationship, but I can’t for the life of me figure out how to do that.
I’m assuming I need to store the tags in the tags table? But if that’s the case, how do I get their ID and insert them as a tag to the article?
Any help is greatly appreciated.
If you are decided on this schema, you should be using the
articles_tagstable as a relationship between entries in thearticlesand thetagstables (1..*). This means that there will be multiple entries in thearticles_tagstable with the samearticle_idand a differenttag_idfor each tag associated with a given article.Alternatively, you could just keep reference to which article a tag belongs to by adding an
article_idfield to yourtagstable.Example:
So these articles_tags entries mean that article with id 4 has tags with id’s 8 & 9. Tag 8 is also used for article id 5.
This is the standard way in SQL to deal with a many to many relationship
(*..*).