Solved

Submit PHP form values and upload multiple image files

Posted on 2011-03-09
7
1,102 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

828 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