[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Submit PHP form values and upload multiple image files

Posted on 2011-03-09
7
Medium Priority
?
1,134 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

656 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