Jump to content

[SOLVED] What is wrong with this?


timmah1

Recommended Posts

I have a form to perform multiple uploads, and then insert the information into a database. The number of uploads depends on membership level. It could be 2 up to 6 max.

 

The problem I'm having, when you upload the files, it inserts 2 items into the database, meaning, it cretes two entries instead of 1 because there are 2 files, if there's 4 uploads, then 4 entries. Make sense?

 

I need it to only put in the database one entry with the file uploads going on the same line.

 

This is my database structure

Field  	Type   	Null  	Default
id 	int(11) 	No  	  	 
date 	date 	No  	0000-00-00  	 
username 	varchar(255) 	No  	  	 
item 	varchar(255) 	No  	  	 
category 	varchar(255) 	No  	  	 
desc 	text 	No  	  	 
price 	varchar(22) 	No  	  	 
photo 	varchar(255) 	No  	  	 
photo1 	varchar(255) 	Yes  	NULL  	 
photo2 	varchar(255) 	Yes  	NULL  	 
photo3 	varchar(255) 	Yes  	NULL  	 
photo4 	varchar(255) 	Yes  	NULL  	 
photo5 	varchar(255) 	Yes  	NULL  	 
shipping 	varchar(255) 	No  	  	 
shipping_details 	varchar(255) 	No  	  	 
sold 	varchar(25) 	No  	  	 

 

Here is the upload, not all of it, but I believe the only part that matters.

 

For($i=0; $i <= $file_uploads-1; $i++) {

If($_FILES['file']['name'][$i]) {
	$file=$i+1;

			Echo("<b>File #".$file.":</b> <a href=\"".$full_url.$file_name[$i]."\" target=\"_blank\">".$full_url.$file_name[$i]."</a><br /><br />\n");
}
include "DB_item_config.php";
$insert = mysql_query("INSERT INTO items VALUES ('NULL', NOW(),'$user[username]','".$_POST['item']."','".$_POST['category']."','".$_POST['desc']."', '".$_POST['price']."', '$file_name[$i]','".$_POST['shipping']."','".$_POST['shipping_details']."','0')") 
or die("Sorry, there was a problem with uploading ".mysql_error());				
}

 

Any help would be greatly appreciated.

Thanks in advance.

Link to comment
Share on other sites

Well, I would suggest that you should have a related table to hold the filenames. So the upload table would create a record of the upload and the files table would list the actual uploads. this way you could allow 1 to many uploads and is very scalable.

 

But, since you asked here is one solution using your current schema. Basically you need to "add up" the file names within the loop and then do the insert after processing all the fiels.:

 

<?php
for($i=0; $i <= $file_uploads-1; $i++) {
   if($_FILES['file']['name'][$i]) {
       $file=$i+1;	
       echo "<b>File #".$file.":</b> <a href=\"".$full_url.$file_name[$i]."\" target=\"_blank\">".$full_url.$file_name[$i]."</a><br /><br />\n";
       //Add uploaded file names to an array.
       $files[] = $file_name[$i];
   }  
}

include "DB_item_config.php";
//Create comma separated value of all uploaded files.
$file_list = implode (', ', $files);
$query = "INSERT INTO items
         VALUES ('NULL', NOW(),'$user[username]','".$_POST['item']."','".$_POST['category']."','".$_POST['desc']."',
                 '".$_POST['price']."','$file_list','".$_POST['shipping']."','".$_POST['shipping_details']."','0')";
$insert = mysql_query($query) or die("Sorry, there was a problem with uploading ".mysql_error());
?>

Link to comment
Share on other sites

Thank you mjdamato, that works perfect!

 

 

I've never done anything like this before, so I have one more question.

How do you display the photo field now that it shows this

1200379039.jpg, 1200376039.jpg

 

How do you go about showing say one photo at top of page and then the other on the bottom of the page?

 

Link to comment
Share on other sites

Again, that is a perfect example of why you should not be constructing your database with multiple values in a single field. Any time you have a many-to-one relationship (such as multiple files with one upload) you should have a separate, related table for the "many" items.

 

But, since you asked. You just need to take the value from the files field and use explode(', ', $value) to get the different file names separated in an array.

 

EDIT: Don't forget to mark the topic as solved.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.