Solved

Uploading multiple images along with their colors

Posted on 2011-02-12
9
235 Views
Last Modified: 2012-05-11
I have a script that successfully uploads multiple images, but the issue is only the first color is being found, the second is somehow becoming lost.  I setup a javascript alert to verify this and sure enough, the second color comes up blank resulting in the following error:

Warning: Illegal offset type in...

Any help is greatly appreciated.
PHP

$i = 0;
		while(list($key, $value) = each($_FILES['image']['name'])) {
			if(!empty($value)) {
				$filename = $value;
				$filename = str_replace(" ", "_", $filename);
				$items = realpath("../images/items");
				//$resize = $items . $filename;
				//create_right_size_image($resize);
				foreach($_FILES['image']['error'] as $key => $error) {
					if($error == UPLOAD_ERR_OK) {
						$tmp_name = $_FILES['image']['tmp_name'][$key];
						$name = $_FILES['image']['name'][$key];
						move_uploaded_file($tmp_name, "$items/$name");
					}
				}
				
				$addImage = sprintf("INSERT INTO prod_images
					(image, prod_id)
					VALUES('%s', %d)", $filename, $prodId);
				$image = mysql_query($addImage) or die("Image $value was not uploaded.");
				
				//foreach($_POST['color'] as $color) {
				$colorarray = $_POST['color'];
				$color = $colorarray[$i];
				echo "
						<script type='text/javascript'>
							alert('$color');
						</script>
						";
					if(!empty($color)) {
						$getImage = sprintf("SELECT image_id, image
							FROM prod_images
							WHERE image = '%s'", $filename);
						$images = mysql_query($getImage) or die("The image id was not grabbed because: " . mysql_error());
						$i = mysql_fetch_array($images);
						$im = (int) $i['image_id'];
						
						$addColor = sprintf("INSERT INTO prod_colors
							(color, image_id, prod_id)
							VALUES('%s', %d, %d)", $color, $im, $prodId);
						$color = mysql_query($addColor) or die("Color $color was not uploaded: " . mysql_error());
					}
				//}
			}
		}


HTML (Part of form)

<?php
            			$max_no_img = 6;
            			
            			for($i=1; $i<$max_no_img; $i++) {
            				echo "
            							<p>
            								<label for='image'>Image $i</label>
            									<input type='file' name='image[]' />
            							</p>
            							<p>
            								<label for='image'>Color $i</label>
            									<input type='text' name='color[]' />
            							</p>
            						";
            			}
            		?>

Open in new window

0
Comment
Question by:pingeyeg
  • 5
  • 4
9 Comments
 
LVL 8

Expert Comment

by:rationalboss
ID: 34879224
In line 26, instead of using $i, can you try using $key?
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 34879256
Changing it from $i to $key returns nothing for both values now.
0
 
LVL 8

Accepted Solution

by:
rationalboss earned 500 total points
ID: 34879322
The error is in line 37. You are using $i as counter and a mysql resource.
Replace that with:

$im = @mysql_fetch_array($images);
$im = (int) $im['image_id'];

Open in new window


And make sure you increment $i :)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:pingeyeg
ID: 34879338
Well, at least that time I got two values, but the problem is they were the same value, which was the first color.
0
 
LVL 8

Expert Comment

by:rationalboss
ID: 34879349
Did you increment $i?
Somewhere in the loop, you need to use $i++;
0
 
LVL 8

Assisted Solution

by:rationalboss
rationalboss earned 500 total points
ID: 34879361
Add $i++; after line 26 :)
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 34879363
Well, I'm incrementing in the form, which is then posted to the page.  In the code above, I'm using a while loop to grab each image posted and inside that while loop, the color is grabbed as well.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 34879380
Fantastic!  Thanks man!
0
 
LVL 8

Expert Comment

by:rationalboss
ID: 34879382
I tried running this here on my side so I added some lines. But this should work. I just wanted to show you that it's working. As for the loop, that's fine if you already use $i in the form since the for loop initializes $i to 1.

<?php
if ($_POST['posted'] == 1) {
	$i = 0;
	while(list($key, $value) = each($_FILES['image']['name'])) {
		echo "key: $key<br />";
		if(!empty($value)) {
			$filename = $value;
			$filename = str_replace(" ", "_", $filename);
			$items = realpath("images/");
			//$resize = $items . $filename;
			//create_right_size_image($resize);
			foreach($_FILES['image']['error'] as $key => $error) {
				if($error == UPLOAD_ERR_OK) {
					$tmp_name = $_FILES['image']['tmp_name'][$key];
					$name = $_FILES['image']['name'][$key];
					#move_uploaded_file($tmp_name, "$items/$name");
				}
			}
			
			$addImage = sprintf("INSERT INTO prod_images
				(image, prod_id)
				VALUES('%s', %d)", $filename, $prodId);
				echo $addImage .' <br />';
			#$image = mysql_query($addImage) or die("Image $value was not uploaded.");
			
			//foreach($_POST['color'] as $color) {
			$colorarray = $_POST['color'];
			$color = $colorarray[$i];
			$i++;
			echo "
					<script type='text/javascript'>
						alert('$color');
					</script>
					";
				if(!empty($color)) {
					$getImage = sprintf("SELECT image_id, image
						FROM prod_images
						WHERE image = '%s'", $filename);
					#$images = mysql_query($getImage) or die("The image id was not grabbed because: " . mysql_error());
					echo $getImage . '<br />';
					$im = @mysql_fetch_array($images);
					$im = (int) $im['image_id'];
					
					$addColor = sprintf("INSERT INTO prod_colors
						(color, image_id, prod_id)
						VALUES('%s', %d, %d)", $color, $im, $prodId);
						echo $addColor .'<br />';
					#$color = mysql_query($addColor) or die("Color $color was not uploaded: " . mysql_error());
				}
			//}
		}
	}
}
?>

<form method="post" enctype="multipart/form-data">
<?php
	$max_no_img = 6;
	
	for($i=1; $i<$max_no_img; $i++) {
		echo "
					<p>
						<label for='image'>Image $i</label>
							<input type='file' name='image[]' />
					</p>
					<p>
						<label for='image'>Color $i</label>
							<input type='text' name='color[]' />
					</p>
				";
	}
?>
<input type="hidden" name="posted" value="1" />
<input type="submit" />
</form>

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 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