Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

upload 3 files to webroot and insert names and description in database

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
I have a form that uploads 3 files (thumb, 4x6, 6x9) at one time into a directory called "images/". This works wonderful. But I need to also insert the names (and other information) into the database. I can't figure out the logic on how to perform this part.
Any help is appreciated

TIA

Here is my current code:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO photo_table (photo_id, photo_client_id, photo_category, photo_name, photo_keyword, photo_desc, photo_thumb_name, photo_4x6_name, photo_6x9_name) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['photo_id'], "int"),
GetSQLValueString($_POST['photo_client_id'], "int"),
GetSQLValueString($_POST['photo_category'], "int"),
GetSQLValueString($_POST['photo_name'], "text"),
GetSQLValueString($_POST['photo_keyword'], "text"),
GetSQLValueString($_POST['photo_desc'], "text"),
GetSQLValueString($_POST['upfile[]'], "text"),
GetSQLValueString($_POST['upfile[]'], "text"),
GetSQLValueString($_POST['upfile[]'], "text"));

mysql_select_db($database_database, $database);
$Result1 = mysql_query($insertSQL, $database) or die(mysql_error());

$numoffile = 3;
// Fix path of your file to be uploaded, don't forget to CHMOD 777 to this folder
$file_dir = "images/";
if ($_POST) {
for ($i=0;$i<$numoffile;$i++) {
if (trim($_FILES['upfile']['name'][$i])!="") {
$newfile = $file_dir.$_FILES['upfile']['name'][$i];
move_uploaded_file($_FILES['upfile']['tmp_name'][$i], $newfile);
$j++;
}
}
}

Input fields of form



<td><input name="upfile[]1" type="file" tabindex="2" size="35" /></td>
</tr>
<tr>
<td height="55"> td>
<td>Photo_desc:</td>
<td><input type="text" name="photo_desc" value="" size="32" /></td>
<td>Select 4 x 6 Photo</td>
<td><input name="upfile[]2" type="file" tabindex="3" size="35" /></td>
</tr>
<tr>
<td height="48">&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>Select 6 x 9 Photo</td>
<td><input name="upfile[]3" type="file" size="35" /></td>
 
why do you do that at all? why not just upload one image and create the other sizes on the fly?
 
Code:
<?
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
	$file_dir  = "images/";
	for($i=0; $i<3;$i++){
	  	//test if upload was complete
		if(isset($_FILES['upfile']['error'][$i]) && $_FILES['upfile']['error'][$i]==0){
			$newfile[$i] = $file_dir.$_FILES['upfile']['name'][$i];
        	move_uploaded_file($_FILES['upfile']['tmp_name'][$i], $newfile[$i]);
		}else{
			$newfile[$i] = 'File not uploaded';
		}
	}
	  $insertSQL = sprintf("
				INSERT 
				INTO 
					photo_table 
				(
					photo_id, 
					photo_client_id, 
					photo_category, 
					photo_name, 
					photo_keyword, 
					photo_desc, 
					photo_thumb_name, 
					photo_4x6_name, 
					photo_6x9_name
				) 
					VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
					GetSQLValueString($_POST['photo_id'], "int"),
					GetSQLValueString($_POST['photo_client_id'], "int"),
					GetSQLValueString($_POST['photo_category'], "int"),
					GetSQLValueString($_POST['photo_name'], "text"),
					GetSQLValueString($_POST['photo_keyword'], "text"),
					GetSQLValueString($_POST['photo_desc'], "text"),
					GetSQLValueString($newfile[0], "text"),
					GetSQLValueString($newfile[1], "text"),
					GetSQLValueString($newfile[2], "text")
		);
	mysql_select_db($database_database, $database);
	mysql_query($insertSQL, $database) or die(mysql_error());

?>

Code:
<td><input name="upfile[0]" type="file"  tabindex="2" size="35" /></td>
    </tr>
    <tr>
      <td height="55"> td>
      <td>Photo_desc:</td>
      <td><input type="text" name="photo_desc" value="" size="32" /></td>
      <td>Select 4 x 6 Photo</td>
      <td><input name="upfile[1]" type="file"  tabindex="3" size="35" /></td>
    </tr>
    <tr>
      <td height="48">&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>Select 6 x 9 Photo</td>
      <td><input name="upfile[2]" type="file"  size="35" /></td>
 
Thanks again, this was extremely helpful. I see my errors. I need to work on my understanding of nesting loops and if statements.
JPadie, you are the man.

 
i think this was more about the naming of form controls to make php treat their submitted values as arrays.
 
Yes, your exactly right. Plus I noticed on my code inside my For loop, I didn't specify that my $newfile was an array and should have been $newfile[$i]. I'm really learning alot in this forum. There are really good answers to a lot of problems, plus you respond really fast. This is great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top