I have three pertinent tables: articles, categories, and art_cat_id, which is a key table: primary key (art_cat_id) and the id's from the two other tables. It's a many to many relationship.
The content is entered into a form and uploaded in an INSERT query automatically which works fine. My problem is that I want to have multiple categories, generated by checkboxes, upload to the key table with the two main id's in as many rows as there are categories checked.
This also works fine except for two irritating bugs:
Problem 1: I always get an extra insert with a blank category
Problem 2: I always get an error message that I forgot to enter a category, even though the INSERT has performed correctly. I think the
php unchecks the check boxes as it makes each query.
The applicable code is:
Code:
// Check for category.
if (!empty($_POST['cat[]'])) {
$cat = $_POST['cat'];
} else {
$cat[] = FALSE;
echo '<p><font color="red">Please enter a category!</font></p>';
}
* * *
if ($t && $h && $lc && $rc && $cat && $date) { // If everything's OK.
// Add the article to the table.
$query = "INSERT INTO articles (title, news, headline, teaser, teaser_hl, content_left, content_right, date) VALUES ('$t', '$n', '$h', '$te', '$th', '$lc', '$rc', '$date')";
$result = @mysql_query ($query); // Run the query.
$aid = @mysql_insert_id(); // Get the article ID.
if ($aid > 0) { // New article has been added.
echo ("The article posted successfully into articles with the title $title and id $aid <br />");
foreach ($cat as $key =>$value) {
{
$query2 = "INSERT INTO art_cat_id (art_id, cat_id) VALUES ('$aid', '$value')";
$result2 = mysql_query ($query2);
$acid = @mysql_insert_id(); //Get the ID for the category-article key table.
if ($acid > 0) { //Entry to key table has been made.
echo ("The art_cat_id key table has been updated for this article with category $value <br />");
} else {
echo ("The art_cat_id key table was NOT updated. <br />");
} // end of IF entry to key table
} // end of category value IF inside foreach
} // end of foreach function
} else { // If first query did not run OK.
echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>'; // Public message.
echo '<p><font color="red">' . mysql_error() . '<br /><br />Query: ' . $query . '</font></p>'; // Debugging message.
}
} else { // If one of the data tests failed.
echo '<p><font color="red">Please try again.</font></p>';
}
} // End of the main submitted conditional.
* * *
<form action="add_article_2.php" method="post">
<fieldset><legend>Fill out the form to enter an article in the appropriate table of the articles database:</legend>
<p><b>News?</b><input type="radio" name="news" value="0" checked="checked">No</input>
<input type="radio" name="news" value="1" >Yes</input> </p><br />
<p>Date (YYYY-MM-DD):</b> <input type="text" name="date" size="10" maxlength="10" value="<?php if (isset($_POST['date'])) echo $_POST['date']; ?>" /></text><br /></p>
<p><b>Category or Categories:</b>
<input type="checkbox" name="cat[]"value="nut" />Nutrition</input>
<input type="checkbox" name="cat[]"value="exe" />Exercise</input>
<input type="checkbox" name="cat[]"value="med" />Medical</input>
<input type="checkbox" name="cat[]"value="men" />Men's Health</input>
<input type="checkbox" name="cat[]"value="wom" />Women's</input>
<input type="checkbox" name="cat[]"value="und" />Under 40</input>
<input type="checkbox" name="cat[]"value="sen" />Seniors</input>
<input type="checkbox" name="cat[]"value="lon" />Live Longer</input>
<input type="checkbox" name="cat[]"value="loo" />Looking Good</input>
<input type="checkbox" name="cat[]"value="sta" />Getting Started</input>
<input type="checkbox" name="cat[]"value="str" />Stress</input>
<input type="checkbox" name="cat[]"value="irr" />Irrel</input>
<input type="checkbox" name="cat[]"value="mis" />Misc</input>
</p>