?
Solved

Submit PHP form values and upload multiple image files

Posted on 2011-03-09
7
Medium Priority
?
1,127 Views
Last Modified: 2013-12-13
I need some help trying to wire up this form. The goal is to be able to upload multiple image files to a specified folder on the server and insert the file name and the other form values into the products table of a MySQL database. IAny help is greatly appreciated.
<form id="uploadForm" action="insert.php" method="POST" enctype="multipart/form-data">
            
            Product Name: <input name="product_name" type="text" value="" /><br /><br />
            Product Description: <input name="product_description" type="text" value="" /><br /><br />
            Price: <input name="price" type="text" value="" /><br /><br />            

                Thumbnail: <input type="file" name="thumbnail" /><br /><br />
                
                Image 1: <input type="file" name="img1" /><br /><br />

                Image 2: <input type="file" name="img2" /><br /><br />

                Image 3: <input type="file" name="img3" /><br /><br />
 
                Image 4: <input type="file" name="img4" /><br /><br />
                
                Image 5: <input type="file" name="img5" /><br /><br />
                <input type="submit" value="Submit" />
            </form>

Open in new window

0
Comment
Question by:steve_landsaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
BhawnaP earned 2000 total points
ID: 35084837
$pname = $_POST['product_name'];
$pdesc = $_POST['product_description'];

$uploaddir1 = 'images/';//folder for images to upload
            
      if($_FILES['thumbnail']['name']!='')
      {
            $thumbnail = $_FILES['thumbnail']['name'];
            if (is_uploaded_file($_FILES['thumbnail']['tmp_name']))
            {
                  ;
            }
            $uploadfile1 = $uploaddir1.$thumbnail;//basename($_FILES['thumbnail']['name']);
            if(move_uploaded_file($_FILES['thumbnail']['tmp_name'], $uploadfile1))
            {
                  ;
            }
      }
      
      if($_FILES['img1']['name']!='')
      {
            $img1 = $_FILES['img1']['name'];
            if (is_uploaded_file($_FILES['img1']['tmp_name']))
            {
                  ;
            }
            $uploadfile2 = $uploaddir1.$img1;//basename($_FILES['img1']['name']);
            if(move_uploaded_file($_FILES['img1']['tmp_name'], $uploadfile2))
            {
                  ;
            }
      }
      
      if($_FILES['img2']['name']!='')
      {
            $img2 = $_FILES['img2']['name'];
            if (is_uploaded_file($_FILES['img2']['tmp_name']))
            {
                  ;
            }
            $uploadfile3 = $uploaddir1.$img2;//basename($_FILES['img2']['name']);
            if(move_uploaded_file($_FILES['img2']['tmp_name'], $uploadfile3))
            {
                  ;
            }
      }
      
      if($_FILES['img3']['name']!='')
      {
            $img3 = $_FILES['img3']['name'];
            if (is_uploaded_file($_FILES['img3']['tmp_name']))
            {
                  ;
            }
            $uploadfile4 = $uploaddir1.$img3;//basename($_FILES['img3']['name']);
            if(move_uploaded_file($_FILES['img3']['tmp_name'], $uploadfile4))
            {
                  ;
            }
      }
      
      if($_FILES['img4']['name']!='')
      {
            $img4 = $_FILES['img4']['name'];
            if (is_uploaded_file($_FILES['img4']['tmp_name']))
            {
                  ;
            }
            $uploadfile5 = $uploaddir1.$img4;//basename($_FILES['img4']['name']);
            if(move_uploaded_file($_FILES['img4']['tmp_name'], $uploadfile5))
            {
                  ;
            }
      }
      
      if($_FILES['img5']['name']!='')
      {
            $img5 = $_FILES['img5']['name'];
            if (is_uploaded_file($_FILES['img5']['tmp_name']))
            {
                  ;
            }
            $uploadfile6 = $uploaddir1.$img5;//basename($_FILES['img5']['name']);
            if(move_uploaded_file($_FILES['img5']['tmp_name'], $uploadfile6))
            {
                  ;
            }
      }
      
      // save query
0
 

Author Comment

by:steve_landsaw
ID: 35087267
Awesome! Now how do I get that inserted into the database? :)
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 35088476
just add the insert statement into each image upload. I'm not sure exactly how your code works but here is a rough example.

if($_FILES['img1']['name']!='')
      {
            $img1 = $_FILES['img1']['name'];
            if (is_uploaded_file($_FILES['img1']['tmp_name']))
            {
                  ;
// Insert into database here
      mysql_query("INSERT INTO images (path,title,type) VALUES ('$path','$title','$extension')")or die(mysql_error());
            }
            $uploadfile2 = $uploaddir1.$img1;//basename($_FILES['img1']['name']);
            if(move_uploaded_file($_FILES['img1']['tmp_name'], $uploadfile2))
            {
                  ;
            }
      }

Just create your insert statement however you wish to have it inserted and put the statement into the above code so as soon as the file has been named and moved then you would run your insert statement.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:steve_landsaw
ID: 35090722
elvin66, won't that create a new row in the database for each image? I want to insert all the form data into one database record.
0
 
LVL 1

Expert Comment

by:BhawnaP
ID: 35091094
Where it says
//save query

the query should be
mysql_query("INSERT INTO tablename (name, desc, thumbnam=il, img1...) VALUES ('$pname','$pdesc','$uploadfile1'....)")or die(mysql_error());
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 35091120
Yes as BhawnaP says put the insert code where it says "save query" at the bottom of your code
0
 

Author Comment

by:steve_landsaw
ID: 35091229
I ended up re-purposing some php logic that I had layin' around and combined it with what you posted first, BhawnaP.

I'm gonna give your latest post a shot as it's a little cleaner that mine.
<?

include('config.php'); 
if (isset($_POST['submitted'])) { 
foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); }

$pname = $_POST['product_name'];
$pdesc = $_POST['product_description'];

$uploaddir1 = '/images/';//folder for images to upload
            
      if($_FILES['thumbnail']['name']!='')
      {
            $thumbnail = $_FILES['thumbnail']['name'];
            if (is_uploaded_file($_FILES['thumbnail']['tmp_name']))
            {
                  ;
            }
            $uploadfile1 = $uploaddir1.$thumbnail;//basename($_FILES['thumbnail']['name']);
            if(move_uploaded_file($_FILES['thumbnail']['tmp_name'], $uploadfile1))
            {
                  ;
            }
      }
      
      if($_FILES['img1']['name']!='')
      {
            $img1 = $_FILES['img1']['name'];
            if (is_uploaded_file($_FILES['img1']['tmp_name']))
            {
                  ;
            }
            $uploadfile2 = $uploaddir1.$img1;//basename($_FILES['img1']['name']);
            if(move_uploaded_file($_FILES['img1']['tmp_name'], $uploadfile2))
            {
                  ;
            }
      }
      
      if($_FILES['img2']['name']!='')
      {
            $img2 = $_FILES['img2']['name'];
            if (is_uploaded_file($_FILES['img2']['tmp_name']))
            {
                  ;
            }
            $uploadfile3 = $uploaddir1.$img2;//basename($_FILES['img2']['name']);
            if(move_uploaded_file($_FILES['img2']['tmp_name'], $uploadfile3))
            {
                  ;
            }
      }
      
      if($_FILES['img3']['name']!='')
      {
            $img3 = $_FILES['img3']['name'];
            if (is_uploaded_file($_FILES['img3']['tmp_name']))
            {
                  ;
            }
            $uploadfile4 = $uploaddir1.$img3;//basename($_FILES['img3']['name']);
            if(move_uploaded_file($_FILES['img3']['tmp_name'], $uploadfile4))
            {
                  ;
            }
      }
      
      if($_FILES['img4']['name']!='')
      {
            $img4 = $_FILES['img4']['name'];
            if (is_uploaded_file($_FILES['img4']['tmp_name']))
            {
                  ;
            }
            $uploadfile5 = $uploaddir1.$img4;//basename($_FILES['img4']['name']);
            if(move_uploaded_file($_FILES['img4']['tmp_name'], $uploadfile5))
            {
                  ;
            }
      }
      
      if($_FILES['img5']['name']!='')
      {
            $img5 = $_FILES['img5']['name'];
            if (is_uploaded_file($_FILES['img5']['tmp_name']))
            {
                  ;
            }
            $uploadfile6 = $uploaddir1.$img5;//basename($_FILES['img5']['name']);
            if(move_uploaded_file($_FILES['img5']['tmp_name'], $uploadfile6))
            {
                  ;
            }
      }
      
//Writes the information to the database
$sql = "INSERT INTO products (product_name,product_description,thumbnail,img1,img2,img3,img4,img5)
VALUES ('$pname', '$pdesc', '$uploaddir1$thumbnail', '$uploaddir1$img1', '$uploaddir1$img2', '$uploaddir1$img3', '$uploaddir1$img4', '$uploaddir1$img5')";

mysql_query($sql) or die(mysql_error()); 
echo "Added row.<br />"; 
echo "<a href='product_list.php'>Back To Listing</a>"; 
} 
?>

Open in new window

0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question