By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

Uploading multiple images from a cms form to server & database

P: 3
Good morning all,

I currently have a form that allows my client to upload one image and a load of other information, such as price, date, name etc.

He has asked if I can have it so that he can upload up to 4 images, and also all the rest of the information.

The images currently go to the server, and the paths with the other data goes into the database.

I have separate fields for each image in the table as I want to have control over how they get displayed.

Can somebody point me to a sensible tutorial, as I'm still learning or even better if they could post their code here so I can change for my use.

I'm still learning, so if you could include the php/mysql, the html and the form that would be awsome.
Mar 20 '12 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
the main change you have to do is send/retrieve the data as a set (4 x image + info).
this is mainly done by adding square brackets to the form control name (ex. name="info[]" => $info_1 = $_POST['info'][0]). on the PHP side you would have then to loop over the set retrieved by the appropriate name.

for a visual distinguation in the HTML you can use the <fieldset> element.
Mar 20 '12 #2

P: 3
Hi Dormilich,

I know this sounds cheeky as but I think I know what your saying, but at this stage it would be great if you could add a bit more to code, so I can see the form, and the mysql/php.

There is another thread on here with a similar answer i think, but having trouble accessing it at the mo.

Cheers
Mar 20 '12 #3

P: 3
Hi again,

I got this code from the web and have tried to use it, with some degree of success.

Expand|Select|Wrap|Line Numbers
  1. <?
  2.  
  3. set_time_limit(300);
  4.   $numoffile = 4;  // Set how many file uploads you want.
  5.  
  6.  
  7.   if ($_POST) {  
  8.     for ($i=0;$i<$numoffile;$i++) {  
  9.       if (trim($_FILES['myfiles']['name'][$i])!="") {  
  10.         $temp_file_location = $_FILES['myfiles']['tmp_name'][$i];
  11.         $old_upload_file = $_FILES['myfiles']['name'][$i];
  12. //        $newfile = $file_dir.$_FILES['myfiles']['name'][$i];
  13.         $newfile = $file_dir. $product_name . ".jpg";
  14.         $filemoved = move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], "./imgdata/stock/$old_upload_file");
  15.  
  16. // run any command here after every file upload.
  17. //$q24=mysql_query("update stock set stock_Image='x', stock_Image2='x', stock_Image3='x', stock_Image4='x' where stock_Id=$sr") or die (mysql_error()); 
  18.  
  19.         $j++;  
  20.       }  
  21.     }  
  22.   }  
  23.   if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>";
  24.   //print $newfile;  
  25.   //print "<form method='post' enctype='multipart/form-data'>";  
  26.   for($i=0;$i<$numoffile;$i++) {  
  27.     print "<input type='file' name='myfiles[]' size='70'><br>";  
  28.   }  
  29.   //print "<input type='submit' name='action' value='Upload'>";  
  30.   //print "</form>";  
  31.  
  32. ?>
  33.  
The file goes to the server, but what I cant work out is how to get the path for each image into the database.

The bit that I put which I know is wrong to try and explain is below.

Expand|Select|Wrap|Line Numbers
  1. // run any command here after every file upload.
  2. //$q24=mysql_query("update stock set stock_Image='x', stock_Image2='x', stock_Image3='x', stock_Image4='x' where stock_Id=$sr") or die (mysql_error()); 
  3.  
And this is what Im currently using to handle the rest of the info in the form.

Expand|Select|Wrap|Line Numbers
  1. $name=$_POST['txtname'];
  2. $desc1=$_POST['e1m1'];        
  3. $meta=$_POST['txtmeta'];
  4. $sr=$_POST['srno1'];
  5. $name=$_POST['txtname'];
  6. $ref=$_POST['Ref'];
  7. $desc=$_POST['e1m1'];
  8. $maker=$_POST['Maker'];
  9. $date=$_POST['Date'];
  10. $weight=$_POST['Weight'];
  11. $height=$_POST['Height'];
  12. $depth=$_POST['Depth'];
  13. $width=$_POST['Width'];
  14. $price=$_POST['txtprice'];
  15. $sold=$_POST['txtsold'];
  16. $active=$_POST['active'];
  17. $pcats=$_POST['pcats'];
  18. $subcats=$_POST['subcats'];
  19.  
  20. $q24=mysql_query("update stock set stock_Name='$name', stock_MetaTitle='$meta', parent_Category='$pcats', sub_Category='$subcats', stock_Ref='$ref', stock_Description='$desc1', stock_Maker='$maker', stock_Date='$date', stock_Weight='$weight', stock_Height='$height', stock_Depth='$depth',stock_Width='$width', stock_Price='$price', stock_Sold='$sold', stock_Active='$active', stock_DateTime='$dt2' where stock_Id=$sr") or die (mysql_error());
  21. $flag=1;
  22. $conf="Data Updated Successfully - Click <a href='http://www.accendsandbox.co.uk/adminSallam/admin_categories.php'>here</a> to continue";
  23. $update="1";
  24. }
  25. else
  26. {
  27. $name=$_POST['txtname'];
  28. $ref=$_POST['Ref'];
  29. $desc=$_POST['e1m1'];
  30. $maker=$_POST['Maker'];
  31. $date=$_POST['Date'];
  32. $weight=$_POST['Weight'];
  33. $height=$_POST['Height'];
  34. $depth=$_POST['Depth'];
  35. $width=$_POST['Width'];
  36. $price=$_POST['txtprice'];
  37. $sold=$_POST['txtsold'];    
  38. $meta=$_POST['txtmeta'];
  39. $active=$_POST['active'];
  40. $pcats=$_POST['pcats'];
  41. $subcats=$_POST['subcats'];
  42.  
  43. $q=mysql_query("insert into stock (stock_Name, stock_MetaTitle, parent_Category, sub_Category, stock_Ref, stock_Description, stock_Maker, stock_Date, stock_Weight, stock_Height, stock_Depth, stock_Width, stock_Price, stock_Sold, stock_Active, stock_DateTime) values('$name','$meta','$pcats','$subcats','$ref','$desc','$maker','$date','$weight','$height','$depth','$width','$price','$sold','$active','$dt2')") or die (mysql_error());    
  44. $conf="Data Inserted Successfully - Click <a href='http://www.accendsandbox.co.uk/adminSallam/admin_stock.php'>here</a> to continue";
  45. $update=1;
  46. }
  47. }
  48.  
Mar 20 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.