Jump to content

[SOLVED] FOREACH error? Makes no sense to me...


NerdConcepts

Recommended Posts

page access looks like this: pictures.php?upload&count=5 (depending on that last number is the number of pictures that will be able to be uploaded).

 

Here is the code:

			if (isset($_GET['upload'])) {

				$counter = $_GET['count'];

				if (isset($_POST['subPicturesUpload'])) {

					$ctr = -1;
					foreach ($_POST['news_id'] as $x) {
						$ctr++;

						move_uploaded_file($_FILES['tmb'][$ctr]['tmp_name'], "images/tmb/" . trim($_FILES['tmb'][$ctr]['name']));
						move_uploaded_file($_FILES['full'][$ctr]['tmp_name'], "images/full/" . trim($_FILES['full'][$ctr]['name']));

						$tmb = $_POST['tmb'][$ctr];
						$full = $_POST['full'][$ctr];
						$query = "INSERT INTO pictures (pic_tmb,pic_full,news_id,pic_date) VALUES ('$tmb','$full','$x',NOW() )";
						$result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
						echo $query . '<br />';
					}
				}
				?>
				<form method="post" action="pictures.php?upload&count=<?PHP echo $counter; ?>" name="pictures_upload" enctype="multipart/form-data">
					<input type="hidden" name="MAX_FILE_SIZE" value="500000"> 
					<span class="TitleGray">Pictures</span><span class="TitleRed">Upload</span><br />
					<br />
					<select name="news_id" class="FormStandard">
						<option>Select News</option>
						<option disabled="disabled">------------------------</option>
						<?PHP
						$qR = "SELECT news_id,news_title FROM news WHERE news_status='CURRENT' ORDER BY news_date ASC";
						$rR = mysql_query($qR) or trigger_error("Query: $qR\n<br />MySQL Error: " . mysql_error());
						while ($rowR = mysql_fetch_array($rR, MYSQL_ASSOC)) {
							echo '<option value="' . $rowR['news_id'] . '">' . $rowR['news_title'] . '</option>\n';
						}
						?>
					</select>
					<br /><br />
					<table border="0" cellpadding="5" cellspacing="0">
						<?PHP
						for ($i = 0; $i < $counter; $i++) {
							?>
							<tr>
								<td>
									Picture Thumb:<br />
									<input type="file" name="tmb[]" class="FormStandard">
								</td>
								<td>
									Picture Full:<br />
									<input type="file" name="full[]" class="FormStandard">
								</td>
							</tr>
							<?PHP
						}
						?>
					</table>
					<br /><br />
					<input type="submit" value="Upload Picture" class="FormButton" />
					<input type="hidden" name="subPicturesUpload" value="TRUE" />
				</form>
				<?PHP
			}

 

When I run the upload function, says that I am using an invalid argument for the foreach command.

" Invalid argument supplied for foreach() "

 

I don't get it, used foreach to do stuff similar to this before. Not sure what is going on. Anyone have a clue?

It is saying that $_POST['news_id'] is not what it is expecting - print_r($_POST) prior to this will show you all teh info you sending to the form; that will allow you to check you are sending the data you think you are!

When I run the upload function, says that I am using an invalid argument for the foreach command.

" Invalid argument supplied for foreach() "

 

yes, because $_POST['news_id'] is not an array... you only select ONE item from the select list.

 

if you want to select multiple items from the select list you should try:

 

<select multiple="multiple" name="news_id[]" class="FormStandard">

 

hope this helps

Well, I have found a lot of problems with the script. I guess you have to have everything as an array. If I used $_FILES['tmb'] or $_FILES['full'] for the foreach() it tries to run the foreach more then it should. But, if I go ahead and make the $_POST['news_id'] an array and have it for the foreach() it works perfectly...not sure why, but it works perfectly now. Plus there was errors in the $tmb and $full. I was using $_POST when I needed to use $_FILES. But, that is all fixed now. Thanks for the info, helped me figure out what was going on.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.