# \[MySQL, PHP\] bug: writing to three tables simultaneously

**URL:** <https://forum.kirupa.com/t/mysql-php-bug-writing-to-three-tables-simultaneously/192236>\
**Category:** programming\
**Created:** [June 27, 2006, 5:47am UTC](https://forum.kirupa.com/t/mysql-php-bug-writing-to-three-tables-simultaneously/192236 "2006-06-27T05:47:27Z")\
**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, 5:47am UTC](https://forum.kirupa.com/t/mysql-php-bug-writing-to-three-tables-simultaneously/192236/1 "2006-06-27T05:47:27Z")

</div>

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:

```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"] == "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

```
