It will help if I explain my database design first:
users (user_id, username, pwd, first_name, family_name, email, admin_priv)
blog (article_id, user_id, title, article, updated, created, image, caption)
tags (tag_id, tag)
blog_tags (id, article_id, tag_id)
The blog_tags table is a composite entity between the blog and tags table.
I have an XHTML form for inserting blog entries, and it contains a text field for inserting tags in the form of a comma delimited string. The string is broken apart using the explode(); function, and is then looped through using foreach to insert each element of the array into the tags table as a separate record.
When I submit the form, it returns no errors, takes me to the proper page, and when I check the tables in my DBMS, the blog table and tags table are OK. The problem is, the form isn’t inserting anything into the blog_tags table, which is also contained in the foreach loop. What do I need to change to make that happen?
Here’s the code:
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
$insertSQL = sprintf("INSERT INTO blog (user_id, title, article, image, caption, created) VALUES (%s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['user_id'], "int"),
GetSQLValueString($_POST['title'], "text"),
GetSQLValueString($_POST['article'], "text"),
GetSQLValueString($_POST['image'], "text"),
GetSQLValueString($_POST['caption'], "text"),
$_POST['created']);
mysql_select_db($database_zoxide_admin, $zoxide_admin);
$Result1 = mysql_query($insertSQL, $zoxide_admin) or die(mysql_error());
$insertGoTo = "list_blog.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
//parse the string of tags (tag_string) into the array $tags
$tags = explode(",",$_POST['tag_string'], 50);
if (!(isset($tags[7]))) {
//assign each element in the tags array to the temp variable tag
foreach($tags as $tag) {
$tag = trim($tag);
$insertSQL = sprintf("INSERT INTO tags (tag) VALUES (%s)",
GetSQLValueString($tag, "text"));
mysql_select_db($database_zoxide_admin, $zoxide_admin);
$Result1 = mysql_query($insertSQL, $zoxide_admin) or die(mysql_error());
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
$insertSQL = sprintf("INSERT INTO blog_tags (tag_id, article_id) VALUES (%s, %s)",
GetSQLValueString($_POST['tag_id'], "int"),
GetSQLValueString($_POST['article_id'], "int"));
mysql_select_db($database_zoxide_admin, $zoxide_admin);
$Result1 = mysql_query($insertSQL, $zoxide_admin) or die(mysql_error());
}//end foreach loop
}//end if first $_POST["MM_insert"] statement
}//end if (isset($tags)) statement
}//end if second $_POST["MM_insert"] statement