It will help if I explain my DB 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. The users table isn’t involved in my problem, so you can ignore it.
I need to write to three tables in a database from the same xhtml form. This is easy enough, but there’s a catch. A comma delimited string of tags needs to be looped through using a foreach statement in order to explode(); it into separate records. For each element in the tag_string array, I need to write to the tags table and the blog_tags table.
The form submits fine with no errors and takes me to the proper page. The problem is, when I check the tags table and the blog_tags table in my DBMS, there’s nothing there. 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"] == "form1")) {
//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"] == "form1")) {
$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 if first $_POST["MM_insert"] statement
}//end foreach loop
}//end if (isset($tags)) statement
}//end if second $_POST["MM_insert"] statement