Solved

Uploading multiple images along with their colors

Posted on 2011-02-12
9
222 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now