Solved

Submit PHP form values and upload multiple image files

Posted on 2011-03-09
7
1,106 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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