Multiple images upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wurtel
Forum Newbie
Posts: 1
Joined: Sat Jan 22, 2005 12:58 pm

Multiple images upload

Post by wurtel »

I recently started learning PHP, so I'm what you could call a big n00b at it still. Nonetheless , I'm trying to create something...


What I want to achieve:
I want to have a script with 4 upload fields & 1 submit button to simultaniously upload 4 images to a folder somewhere on the server. - Done, no problem

But I also need the filename of each image to be stored in the database in a different row, under the same ID - Not done, and my big problem

(see image below for some more clarification)
Image


Some extra info:
Let's assume our files are image1.jpg, image2.jpg, image3.jpg & image4.jpg

The script uploads the files and inserts their filename in the database, but it inserts:

- image1.jpg under row image0 in id 4
- image2.jpg under row image1 in id 5
- image3.jpg under row image2 in id 6
- image4.jpg under row image3 in id 7

(see image below for some more clarification)
Image

This is what I have for my database, I fear the problem may lie here?

Code: Select all

CREATE TABLE test (
  id tinyint (4) NOT NULL auto_increment,
  image0 varchar (250) NOT NULL default '',
  image1 varchar (250) NOT NULL default '',
  image2 varchar (250) NOT NULL default '',
  image3 varchar (250) NOT NULL default '',
  PRIMARY KEY (id),
  UNIQUE id (id)
);

INSERT INTO test VALUES (2,'yarr0.jpg','yarr1.jpg','yarr2.jpg','yarr3.jpg');
INSERT INTO test VALUES (3,'yii0.jpg','yii1.jpg','yii2.jpg','yii3.jpg');
This is my php code so far

Code: Select all

<html>
<body>

<?php require ('config.php'); // data to connect to the DB ?> 

<?php	
    echo "<form name="image_upload" enctype="multipart/form-data" method="post" action="$PHP_SELF">\n";

    $fields = 4; // number of upload fields
	
    for($x=0;$x<$fields;$x++)&#123; // start of dynamic form (loop)
        echo "<input name="image$x" size="50" type="file" id="image$x"><br>\n";

    &#125; // end for loop to create 4 input fields
        echo "<input type="submit" name="submit" value="Verstuur">\n";
        echo "</form>";

if($_POST&#1111;'submit']) &#123; // form has been submitted
	
    for($x=0;$x<$fields;$x++)&#123; // start for loop
	
    $file_name = $_FILES&#1111;'image'. $x]&#1111;'name'];
    $uploads = 'upload'; // directory to upload to
    $copy = copy($_FILES&#1111;'image'. $x]&#1111;'tmp_name'],$uploads.'/'.$file_name);
		
        if($copy) &#123; // check if successfuly copied
            $sql = "INSERT INTO test (image$x) VALUES ('$file_name')";	
            $result = mysql_query($sql); // run SQL query against DB
            echo "$file_name | uploaded successfuly!<br>\n";
	
        &#125;else&#123;
            echo "$file_name | could not be uploaded!<br>\n";
        &#125;
	
    &#125; // end for loop
	
&#125; // end of submit statement


?>

</body>
</html>
I know there's no virtually no validation yet, but my first priority is to get it to work. If there's anyone that can help me, please have a look because I'm desperate and in dire need of this script!

Thanks in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your table structure does not allow for multiples of the same id, and this is a good thing.. What you may want to do is create a second table to link them together. Read here for more concept details: http://www.oreilly.com/catalog/javadtab ... r/ch02.pdf
Post Reply