Solved

Uploading multiple images along with their colors

Posted on 2011-02-12
9
247 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to dynamically set the form action using jQuery.

752 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