Jump to content

Recommended Posts

Heres the error I got...

Warning: Invalid argument supplied for foreach() in /home/content/r/o/c/rockywiu/html/test.php on line 47

 

I am pretty sure i am getting this error because when there are no errors then the $bad_format or $blank_array arrays dont have any info in them. what im not sure is what to do about that.

 

Heres the code..

<?php include("includes.php"); ?>
<?php include("header.php"); ?>

<?
switch ($do)
{
case "insert":
	/* Set up array of field labels */
	$label_array = array ("price" => "Price","title" => "Title","description" => "Description","email" => "E-Mail");
	foreach ($_POST as $field => $value)
	{
		/* Check for blank fields */
		if ($value == "")
		{
			/* check all except type,subtype and reply for blank fields */
			if ($field != "type" or $field != subtype or $field != "reply")
			{
				$blank_array[$field] = "blank";
			}
		}
		elseif ($field == "email")
		{
			if(!ereg(".+\@.+\..+",$_POST[$field]))
			{
				$bad_format[$field] =  "bad";
			}
		}
	}
	/* if any fields were not ok, display error message and form */
	if (@sizeof(blank_array) > 0 or @sizeof(bad_format) > 0)
	{
		if (@sizeof(blank_array) > 0)
		{
			/*display message for missing information*/
			echo "you didnt fill out one or more required fields. you must enter:<br>";
			/*display list of missing information*/
			foreach ($blank_array as $field => $value)
			{
				echo "    $label_array[$field]<br>";
			}
		}
		if (@sizeof(bad_format) > 0)
		{
			/*display message for bad information*/
			echo "One or more fields have information that appears incorrect. correct the format for:<br>";
			/*display list of bad information*/
			foreach ($bad_format as $field => $value)
			{
				echo "    $label_array[$field]<br>";
			}

		}
	exit();
	}


	$today = date("Y-m-d");
	$query = sprintf("INSERT INTO Posting (postdate,type,subtype,price,title,description,email,reply) 
                  VALUES ('%s','%s','%s','%s','%s','%s','%s','%s')",
			  $today, $_POST['type'],$_POST['subtype'],$_POST['price'],$_POST['title'],$_POST['description'],
                  $_POST['email'],$_POST['reply']);

	dbcon($query); // <-- database connection function located on includes.php
echo "upload success.";
break;
case "form":
	unset($_GET['do']); 
	echo "Category: "; posting_type($_POST['type']); echo "<br>";
	echo "Sub Category: ";  posting_subtype($_POST['subtype']);  echo "<br><br>"; ?>
	<form  action="<?php echo $_SERVER['PHP_SELF']; ?>?do=insert" method="post">
	<input name="type" type="hidden" value="<?php echo $_POST['type'];?>" />
	<input name="subtype" type="hidden" value="<?php echo $_POST['subtype'];?>" />
	Title<input name="title" type="text"><br>

	<?php
	switch ($_POST['type'])
	{
		case "1":
			if ($_POST['subtype'] == 1 or $_POST['subtype'] == 6){$displayprice = 0;}
			else {$displayprice = 1;}
			break;
		case "2":
			$displayprice = 1;
			break;
		case "3":
			$displayprice = 0;
			break;
		case "4":
			$displayprice = 1;
			break;
		case "5":
			$displayprice = 1;
			break;
	} 

	if ($displayprice == 0){ echo "<font color='#CCCCCC'>Price</font><input name='price' type='text' disabled><br><br>";}
					  else { echo "Price<input name='price' type='text'><br><br>";} ?>


	Description<br>
	<textarea name="description" cols="40" rows="4"></textarea><br><br>

	Email<input name="email" type="text"><br>
	Verify<input name="vemail" type="text"><br><br>

	<input name="reply" type="radio" value="0" checked>Reply to my E-Mail<br>
	<input name="reply" type="radio" value="1">Do not show my e-mail<br><br>

	Upload image  (Coming Soon..)<br>
	<input name="h" type="text" disabled><input name="browse" type="button" disabled value="Browse..."><br><br>
	<input name="submit" type="submit" value="Submit">
	</form>
	<?php
break;
case "sbtype":
	unset($_GET['do']); ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=form" method="post">
	<select name="subtype" >
	<option value="0">Select One..</option>
	<?php 
	$query2 = "Select * FROM PostingSubType WHERE ptype= '$_POST[type]'";
	dbcon($query2); 
	while ($row = mysql_fetch_array($result))
	{
	extract($row); ?>
	<option value="<?php echo $psubtype; ?>"><?php echo $postingsubtype; ?></option>
	<?php } ?>
	</select>
	<input name="type" type="hidden" value="<?php echo $_POST['type'];?>" />
	<input name='submit' type='submit' value='submit'>
	</form>	<?php
break;
default; ?>
	<form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=sbtype" method="post">
	<select name="type" >
	<option value="0">Select One..</option>
	<?php 
	$query = "Select * FROM PostingType";
	dbcon($query);

	while ($row = mysql_fetch_array($result))
	{
	extract($row); ?>
	<option value="<?php echo $key; ?>"><?php echo $name; ?></option>
	<?php } ?>
	</select>
	<input name="submit" type="submit" value="submit">
	</form>
	<?php
break;
	}?>

Link to comment
https://forums.phpfreaks.com/topic/38738-solved-foreach-trouble/
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.