Forum Moderators: coopster

Message Too Old, No Replies

Foreach problem

         

jeffgman

1:07 am on Sep 21, 2004 (gmt 0)

10+ Year Member



The following script was running on a OS X machine running Apache 1.x and PHP 4.x. I just moved the script over to a Linux machine running Apache 2.0.51 and PHP 4.3.8, and now the foreach line does not work. It says Warning: Invalid argument supplied for foreach() in /var/www/localhost/htdocs/warehouse/whse_order_submit.php on line 39

Here is the script:


<?php
//get the variables from the other page
$id=$_POST['id_no'];
$sku=$_POST['sku'];
$description=$_POST['description'];
$order=($_POST['order_qty']);
$store=$_POST['store'];
$comment=$_POST['comment'];

$ip=$REMOTE_ADDR;

//check if store is empty
if(empty($store)) {
die("Store is required. Please press the back button and enter in your store.");
}

//connect to the database
require('../inc/database_conn.php');

//$month=getdate(mon);
//$day=getdate(mday);
//$year=getdate(year);
//$today=$year . "-" . $month . "-" . $day
$today=date('Y-m-d');

// create the unique order number

$headerqry = "INSERT INTO misc_order_header (order_date, store, comment, ip) VALUES ('$today', '$store', '$comment', '$ip')";

mysql_query($headerqry, $conn) or die(mysql_error());

$order_no=mysql_insert_id();

foreach($order_qty as $id_no => $val) {

if($order_qty[$id_no] > 0) {

$detailqry = "INSERT INTO misc_order_detail (order_no, id_no, sku, order_qty, order_date, store) VALUES ('$order_no', '$id_no', '$sku', '".$order_qty[$id_no]."', '$today', '$store')";
mysql_query($detailqry, $conn) or die(mysql_error() . "<BR>" . $detailqry);
}

}

Thanks for any help you can offer me.

jamie

6:02 am on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi jeff,

>> Invalid argument supplied for foreach()

normally means the first argument in the foreach construction is not an array.

i notice you put the var $_POST['order_qty'] into $order - so you should be looping through

foreach ($order as ...)

the reason why it worked on the osx machine is probably because register globals [es2.php.net] were turned on:

this means to your old environment (osx) $order_qty is the same as $_POST['order_qty'] - but on the new linux box you have to specifically access the $_POST value - because in all recent versions of php register globals [es2.php.net] is turned off by default.

further down your script you try to capture the $REMOTE_ADDR; this won't work either - you will need $_SERVER['REMOTE_ADDR'].

google for 'php register globals' or check the manual from the link above

hth

<edit misspelling>

jeffgman

3:49 pm on Sep 21, 2004 (gmt 0)

10+ Year Member



Thank you for the reply. I got a response last night which told me the same thing. I have since switched the variable name to order_qty and everything is working correctly now.

I also noticed my $REMOTE_ADDR problem last night. After reviewing the PHP website I changed the line to
$ip = getenv(REMOTE_ADDR);

Will that do the same as
$ip = $_SERVER['REMOTE_ADDR'];

or should I use the Server method?

Thanks,
Jeff

coopster

6:25 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



getenv() [php.net] does not work in ISAPI mode.

On the other hand, the $_SERVER [php.net] variables are created by the webserver. There is no guarantee that every webserver will provide any of these; servers may omit some, or provide others. That said, a large number of the $_SERVER variables are accounted for in the CGI 1.1 specification [hoohoo.ncsa.uiuc.edu], so you should be able to expect those.

sonjay

12:04 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



coopster -- Once I've checked my server for the $_SERVER variables it creates, would it be the case that I can freely use those variables anytime I want for sites hosted on that server? I would only need to consider that issue again when I'm working on sites on a different server? (Or if my host changes their server configuration, obviously.)

coopster

3:29 pm on Sep 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, yes, for the most part. But keep in mind that the user agent can throw some punches at you, too. Take, for example,
HTTP_REFERER
:


'HTTP_REFERER'

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

sonjay

4:00 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



Oh, right, except for information that's actually provided by the user/user agent, not the server itself. Makes perfect sense.

In looking over the list of $_SERVER vars, there are several that are provided by the user agent. So I could really only rely on the ones that are actually set by the server itself, like HTTP_HOST, DOCUMENT_ROOT, SERVER_NAME and such.