Uploading multiple images from a cms form to server & database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • accend
    New Member
    • Mar 2012
    • 3

    Uploading multiple images from a cms form to server & database

    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.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    • accend
      New Member
      • Mar 2012
      • 3

      #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

      Comment

      • accend
        New Member
        • Mar 2012
        • 3

        #4
        Hi again,

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

        Code:
        <?
         
        set_time_limit(300);
          $numoffile = 4;  // Set how many file uploads you want.
         
         
          if ($_POST) {  
            for ($i=0;$i<$numoffile;$i++) {  
              if (trim($_FILES['myfiles']['name'][$i])!="") {  
        		$temp_file_location = $_FILES['myfiles']['tmp_name'][$i];
        		$old_upload_file = $_FILES['myfiles']['name'][$i];
        //        $newfile = $file_dir.$_FILES['myfiles']['name'][$i];
                $newfile = $file_dir. $product_name . ".jpg";
                $filemoved = move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], "./imgdata/stock/$old_upload_file");
         
        // run any command here after every file upload.
        //$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()); 
         
                $j++;  
              }  
            }  
          }  
          if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>";
          //print $newfile;  
          //print "<form method='post' enctype='multipart/form-data'>";  
          for($i=0;$i<$numoffile;$i++) {  
            print "<input type='file' name='myfiles[]' size='70'><br>";  
          }  
          //print "<input type='submit' name='action' value='Upload'>";  
          //print "</form>";  
         
        ?>
        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.

        Code:
        // run any command here after every file upload.
        //$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());
        And this is what Im currently using to handle the rest of the info in the form.

        Code:
        $name=$_POST['txtname'];
        $desc1=$_POST['e1m1'];		
        $meta=$_POST['txtmeta'];
        $sr=$_POST['srno1'];
        $name=$_POST['txtname'];
        $ref=$_POST['Ref'];
        $desc=$_POST['e1m1'];
        $maker=$_POST['Maker'];
        $date=$_POST['Date'];
        $weight=$_POST['Weight'];
        $height=$_POST['Height'];
        $depth=$_POST['Depth'];
        $width=$_POST['Width'];
        $price=$_POST['txtprice'];
        $sold=$_POST['txtsold'];
        $active=$_POST['active'];
        $pcats=$_POST['pcats'];
        $subcats=$_POST['subcats'];
        
        $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());
        $flag=1;
        $conf="Data Updated Successfully - Click <a href='http://www.accendsandbox.co.uk/adminSallam/admin_categories.php'>here</a> to continue";
        $update="1";
        }
        else
        {
        $name=$_POST['txtname'];
        $ref=$_POST['Ref'];
        $desc=$_POST['e1m1'];
        $maker=$_POST['Maker'];
        $date=$_POST['Date'];
        $weight=$_POST['Weight'];
        $height=$_POST['Height'];
        $depth=$_POST['Depth'];
        $width=$_POST['Width'];
        $price=$_POST['txtprice'];
        $sold=$_POST['txtsold'];	
        $meta=$_POST['txtmeta'];
        $active=$_POST['active'];
        $pcats=$_POST['pcats'];
        $subcats=$_POST['subcats'];
        								
        $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());	
        $conf="Data Inserted Successfully - Click <a href='http://www.accendsandbox.co.uk/adminSallam/admin_stock.php'>here</a> to continue";
        $update=1;
        }
        }

        Comment

        Working...