INSERT into Multiple Tables: Works, But Misses One Field

I read some previous posts in this forum about inserting records into multiple tables and adapted one suggestion with this:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO images (image_id, image) VALUES (%s, %s)",
																							GetSQLValueString($_POST['image_id'], "int"),
                       GetSQLValueString($_POST['image'], "text"));

  mysql_select_db($database_Tr_adconr25_jut, $Tr_adconr25_jut);
  $Result1 = mysql_query($insertSQL, $Tr_adconr25_jut) or die(mysql_error());

$new_image_id = mysql_insert_id();

$insertSQL = sprintf("INSERT INTO books_on_sale (image_id, teaser, book, author) VALUES ($new_image_id, %s, %s, %s)",
                       GetSQLValueString($_POST['image_id'], "int"),
                       GetSQLValueString($_POST['teaser'], "text"),
                       GetSQLValueString($_POST['book'], "text"),
                       GetSQLValueString($_POST['author'], "text"));
																							
  mysql_select_db($database_Tr_adconr25_jut, $Tr_adconr25_jut);
  $Result1 = mysql_query($insertSQL, $Tr_adconr25_jut) or die(mysql_error())

;

My problem is that everything posts fine with the exception of the table ‘books_on_sale’ “teaser” field. It posts as NULL and I’m not sure why.

I’ve tried removing the “$new_image_id” and replacing it with ‘%s’ and the “teaser” field will get posted, but then the “image_id” field in the ‘books_on_sale’ table will post as NULL.

Can someone tell me where I’m going wrong?

Check your assumptions. You assume your sql query is correct, and has a value for teaser. Check it. Use echo.

If you find that it’s missing the value, then the next logical step is to check the place where the value comes from…your post variable. Again, use echo.

I’ve made a slight modification to my tables based upon a recommendation from r937, which seems a more efficient route, but I’m still encountering INSERT issues. The only one being that the “images” table ‘image’ field sets to NULL.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO books_on_sale (teaser, book, author) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['teaser'], "text"),
                       GetSQLValueString($_POST['book'], "text"),
                       GetSQLValueString($_POST['author'], "text"));

  mysql_select_db($database_Tr_adconr25_jut, $Tr_adconr25_jut);
  $Result1 = mysql_query($insertSQL, $Tr_adconr25_jut) or die(mysql_error());
		
		$new_bos_id = mysql_insert_id();

		$insertSQL = sprintf("INSERT INTO images (bos_id, image) VALUES ($new_bos_id, %s)",
                       GetSQLValueString($_POST['bos_id'], "int"),
                       GetSQLValueString($_POST['image'], "text"));
																							
		mysql_select_db($database_Tr_adconr25_jut, $Tr_adconr25_jut);
  $Result1 = mysql_query($insertSQL, $Tr_adconr25_jut) or die(mysql_error());

I’ve checked my form and don’t see where the problem lies:

<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" onsubmit="checkFileUpload(this,'GIF,JPG,JPEG,BMP,PNG,gif,jpg,jpeg,bmp,png',false,5000,'','','','','','');return document.MM_returnValue">
<table width="80%" align="center" class="list">
<tr valign="baseline">
<td align="right" nowrap class="right">Upload Image:</td>
<td><input name="image" type="file" id="image" onchange="checkOneFileUpload(this,'GIF,JPG,JPEG,BMP,PNG,gif,jpg,jpeg,bmp,png',false,5000,'','','','','','')" size="40" maxlength="40" /></td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap class="right">Teaser:</td>
<td><textarea name="teaser" cols="50" rows="5"></textarea>
</td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap class="right">Book Title:</td>
<td><textarea name="book" cols="50" rows="5"></textarea>
</td>
</tr>
<tr valign="baseline">
<td align="right" nowrap class="right">Author:</td>
<td><input type="text" name="author" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="submit" class="btn" value="Insert"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>

A file does get uploaded, but the name of the file does not record in the database table “images” in the ‘image’ field.

The name of the image won’t be available in the $_POST array, it will be in the $_FILES array. You’ll need to do some other things though, like move the file to a permanent location.

There’s some more info here, in particular example 2
http://www.php.net/manual/en/features.file-upload.post-method.php

Uploading files can bring a lot of non obvious security issues. Consider reading the following if those who will be uploading might be malacious.
http://www.scanit.be/uploads/php-file-upload.pdf

This wasn’t exactly what I was looking for. I’m just trying to figure out why the filename is not recording in the ‘images’ table of the ‘image’ field.

There’s no problem posting everything else, with the exception of the field ‘image’.

Did this post make sense to you?