Solved

Submit PHP form values and upload multiple image files

Posted on 2011-03-09
7
1,095 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
BhawnaP earned 500 total points
Comment Utility
$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
Comment Utility
Awesome! Now how do I get that inserted into the database? :)
0
 
LVL 20

Expert Comment

by:Mark Brady
Comment Utility
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:steve_landsaw
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now