timmah1 Posted January 14, 2008 Share Posted January 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85976-solved-what-is-wrong-with-this/ Share on other sites More sharing options...
GingerRobot Posted January 14, 2008 Share Posted January 14, 2008 Whats wrong with this is that your inserting inside your for loop. If you only want one entry, the database insert needs to be done outside of the loop Quote Link to comment https://forums.phpfreaks.com/topic/85976-solved-what-is-wrong-with-this/#findComment-439046 Share on other sites More sharing options...
Psycho Posted January 14, 2008 Share Posted January 14, 2008 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()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/85976-solved-what-is-wrong-with-this/#findComment-439048 Share on other sites More sharing options...
timmah1 Posted January 14, 2008 Author Share Posted January 14, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/85976-solved-what-is-wrong-with-this/#findComment-439061 Share on other sites More sharing options...
Psycho Posted January 14, 2008 Share Posted January 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85976-solved-what-is-wrong-with-this/#findComment-439064 Share on other sites More sharing options...
timmah1 Posted January 14, 2008 Author Share Posted January 14, 2008 Thank you very much! Much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/85976-solved-what-is-wrong-with-this/#findComment-439067 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.