# \[PHP, MySQL\] writing to multiple tables

**URL:** <https://forum.kirupa.com/t/php-mysql-writing-to-multiple-tables/192240>\
**Category:** programming\
**Created:** [June 27, 2006, 6:07am UTC](https://forum.kirupa.com/t/php-mysql-writing-to-multiple-tables/192240 "2006-06-27T06:07:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![redrum87](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@redrum87](https://forum.kirupa.com/u/redrum87)\
**Post date:** [June 27, 2006, 6:07am UTC](https://forum.kirupa.com/t/php-mysql-writing-to-multiple-tables/192240/1 "2006-06-27T06:07:44Z")

</div>

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:

```php
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

```
