Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Uploading multiple images along with their colors

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
pingeyeg
Asked:
pingeyeg
  • 5
  • 4
2 Solutions
 
rationalbossCommented:
In line 26, instead of using $i, can you try using $key?
0
 
pingeyegAuthor Commented:
Changing it from $i to $key returns nothing for both values now.
0
 
rationalbossCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
pingeyegAuthor Commented:
Well, at least that time I got two values, but the problem is they were the same value, which was the first color.
0
 
rationalbossCommented:
Did you increment $i?
Somewhere in the loop, you need to use $i++;
0
 
rationalbossCommented:
Add $i++; after line 26 :)
0
 
pingeyegAuthor Commented:
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
 
pingeyegAuthor Commented:
Fantastic!  Thanks man!
0
 
rationalbossCommented:
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!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now