How do I insert a record and upload a file at the same time?

Hi All,

As a novice to PHP (15 years programming AS400s and RPG, also Delphi) I find I’m having a problem when trying to to insert a record and upload a file at the same time, I am using the Dreamweaver insert record and then trying to put the file upload code in, but seem to be getting in all wrong, does anybody have a single field insert and file upload that I can have a look at to see where I am going wrong…

Thanks

Ian

Could you show us the code that Dreamweaver is generating to do this task so that we might better evaluate what it is you are trying to do?

Hi

Here is the code I that is generated within Dreamweaver…I guess I should be coding manually and not using dreamweaver because it seems to make things unreadable!!

What I want is when I in insert a record via Mysql into a database (product detail) I also at exactly the same time want to upload the product image as selected in the browse file value to the server via php… can this be done in the same page or do I have to call a new php page to process the code to upload?

I have the code and can do this seperately, in fact I can do both items seperately but not together!!

I have not put the PHP file upload code in this bit, the question is where should it go?

Many Thanks

Ian

[code]<?php require_once('Connections/TheGoatCompany.php'); ?>

<?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO categorydet (seq, productimage, category, ProductName, Description, ProductPrice, uomovride) VALUES (%s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['seq'], "int"), GetSQLValueString($_POST['productimage'], "text"), GetSQLValueString($_POST['category'], "text"), GetSQLValueString($_POST['ProductName'], "text"), GetSQLValueString($_POST['Description'], "text"), GetSQLValueString($_POST['ProductPrice'], "text"), GetSQLValueString($_POST['uomovride'], "text")); mysql_select_db($database_TheGoatCompany, $TheGoatCompany); $Result1 = mysql_query($insertSQL, $TheGoatCompany) or die(mysql_error()); } mysql_select_db($database_TheGoatCompany, $TheGoatCompany); $query_product = "SELECT * FROM categorydet"; $product = mysql_query($query_product, $TheGoatCompany) or die(mysql_error()); $row_product = mysql_fetch_assoc($product); $totalRows_product = mysql_num_rows($product); mysql_select_db($database_TheGoatCompany, $TheGoatCompany); $query_Catergories = "SELECT * FROM categories"; $Catergories = mysql_query($query_Catergories, $TheGoatCompany) or die(mysql_error()); $row_Catergories = mysql_fetch_assoc($Catergories); $totalRows_Catergories = mysql_num_rows($Catergories); ?> Untitled Document
Seq:
Product image:
Category:
Product Name:
Description: Cats <?php do { ?> <?php echo $row_Catergories['catname']?> <?php } while ($row_Catergories = mysql_fetch_assoc($Catergories)); $rows = mysql_num_rows($Catergories); if($rows > 0) { mysql_data_seek($Catergories, 0); $row_Catergories = mysql_fetch_assoc($Catergories); } ?>
Product Price:
UOM overide:
 

 

<?php mysql_free_result($product);

mysql_free_result($Catergories);
?>[/code]

MOD EDIT: Added code tags

The question is: do you want the upload and the insert to be two completely separate things, or do you want them to share certain info (say, give the uploaded file a name that should also be in the inserted data)? It is very much possible to combine the uploading of a file and inserting data into a database.

What I see in your code, is that your query takes an %s argument at one point where it expects an integer ;) check your $insertSQL variable. I haven’t exactly looked much further in your code, but after answering my questions, I guess it will make it easier to help you out a bit.

Sponsor our Newsletter | Privacy Policy | Terms of Service