I have an upload form with 3 fields (attached screenshot):
- Image upload field,-Events link field, Location field
There's a javascript option to dynamically add additional instances of these fields if a user has multiple files.
The multiple image uploads currently work with the code I have now, but how do I insert multiple image paths, events field info, location field info into my database i have setup depending on the amount of additional fields the user selects?
I feel as though somewhere in my php code I need to have some kind of statements that says
"While there are images to upload, for each image, insert the image path, event link, and location field into the database as a new record until there are no more images to upload"
HTML Form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
Image:
<input type="file" name="images[]" /> <br />
Events Link:
<input type="text" name="events[]" /><br />
Location:
<textarea name="location[]" cols="20" rows="3" ></textarea>
<input type="submit" name="submit" value="Submit" class="button black medium">
<input type="reset" name="reset1" class="button black medium">
<p id="addnew"><a href="javascript:new_link()">Add New </a></p>
</form>
PHP Code:
if (isset($_POST['submit'])) {
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '\\images\\locs\\resized\\'; //set upload directory
for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
$number_of_file_fields++;
echo $number_of_file_fields;
if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['images']['name'][$i];
if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
$number_of_moved_files++;
}
}
}
echo "<p style='background-color:#DDD; border:1px solid #000; padding: 8px;'>
<strong>Number of File fields created</strong>: ". $number_of_file_fields ."<br/>
<strong>Number of files submitted</strong>: ". $number_of_uploaded_files . "<br/>
<strong>Number of successfully moved files</strong>: ". $number_of_moved_files . "<br/>
<strong>File Names are</strong><br/>" . implode(',', $uploaded_files);
}?>
My database fields are: id, image name, path, events_link, location
THANK YOU SO MUCH FOR HELP IN ADVANCE!