Solved

Uploading multiple images along with their colors

Posted on 2011-02-12
9
255 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 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