Thanks for taking the time to read this. Let me start by saying I'm a php newb so be gentle!

I have a form that is used to insert data into my database to display info and images in a portfolio section on my site. In the form, the input fields for the thumbnail files and the larger images that are associated with the thumbnails are dynamic; meaning, I can add as many input fields as I need by clicking an add link. I have everything working correctly with the exception of the foreach loop used to insert the image names into the database.

What is happening is that let's say I upload 4 thumbnails and the 4 larger images associated with those thumbnails. In my table, it's putting the 4 thumbnails in their own row and the 4 large images on their own row (please see attached file db_insert_incorrect.jpg).


What I need is for each thumbnail and the large image it's associated with on the same row.(please see attached file db_insert_correct.jpg. I photoshopped it to show what I'm wanting).

Here's the foreach loop in question (I only included the loop that was causing issues since everything else is working. I can post the rest of the code if need be):

if (isset($_POST['btnSubmit'])) {

//.........

foreach ( $_FILES['small_tn_']['name'] as $key=>$value1 ) {
			
$sql_small_tn = sprintf("INSERT INTO overlay (small_tn) VALUES ('%s')",($value1));  
$result_small_tn = $db->query($sql_small_tn);
}
	

foreach ( $_FILES['large_image_']['name'] as $key=>$value2) {

$sql_large_img = sprintf("INSERT INTO overlay (large_img) VALUES ('%s')",($value2));  
$result_large_img = $db->query($sql_large_img);
}
 

}

//........

Any help would be greatly appreciated. This is my first post so if I broke any rules, I apologize.

Thanks!

Recommended Answers

All 4 Replies

try:

if( isset($_POST['btnSubmit']) )
{

	//.........

	foreach ( $_FILES['small_tn_']['name'] as $key=>$value1 )
	{
		$smallImage=$_FILES['small_tn_']['name'][$key];
		$largeImage=$_FILES['large_image_']['name'][$key];
		$sql_small_tn = sprintf("INSERT INTO `overlay`(`small_tn`,`large_img`) VALUES ('%s','%s')",$smallImage, $largeImage );  
		$db->query($sql_small_tn);
	}
}

Hey, thanks for the quick reply! That works perfectly!

Thanks for saving me another headache. I was at it all day yesterday, I just didn't know the correct syntax. Would you mind explaining to me what exactly you did to correct my error?

Again, I really appreciate you taking the time to help me out!

The problem is that you were processing the images separately. One foreach for the thumbnails and one for the large images. You need to do both at once since you need two per record/row.

Based on your post, my guess is that in your HTML markup you have something SIMILAR to:

<div>Image 1:
Thumbnail:<input type="file" name="small_tn_[]">
Large Image:<input type="file" name="large_image_[]">
</div>
<div>Image 2:
Thumbnail:<input type="file" name="small_tn_[]">
Large Image:<input type="file" name="large_image_[]">
</div>

OR:

<div>Image 1:
Thumbnail:<input type="file" name="small_tn_[0]">
Large Image:<input type="file" name="large_image_[0]">
</div>
<div>Image 2:
Thumbnail:<input type="file" name="small_tn_[1]">
Large Image:<input type="file" name="large_image_[1]">
</div>

When you upload the images, then: $_FILES['small_tn_']['name'][0] will have the name of the first Thumbnail $_FILES['small_tn_']['name'][1] will have the name of the second Thumbnail $_FILES['large_image_']['name'][0] will have the name of the first Large Image $_FILES['large_image_']['name'][1] will have the name of the second Large Image

If you look closer, the thumbnails are "connected/related" to the large image via the index. The zero in the thumbnail corresponds to the zero in the large image.

Likewise, the one in the thumbnail corresponds with the one in the large image.

That's why you need to use the $key in the foreach. On every iteration it will increment (starting from zero).

Thus, using $key , on lines 8 and 9 of my post I can get the "related" images.

Ok, I see. That makes better sense to me now. I wish I would have know this yesterday, ha!

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.