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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.