Zen Cart Logo
Forums / General Questions / PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

Views: 4,301

Results 1 to 8 of 8
3 Jan 2013, 5:35 PM
#1
limelites avatar

limelites

Totally Zenned

Join Date:
Jan 2009
Posts:
2,085
Plugin Contributions:
0

PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

I've noticed we're getting the following error each time we use the update button when editing products:

PHP Warning: Invalid argument supplied for foreach() in /admin/includes/modules/update_product.php on line 294

It point to this section:

	$sql_del = "delete from product_ebay_shipping_methods where shipping_type = 'International' and products_id = ". $products_id;
	$db->Execute($sql_del);
	 
	if ($_POST['shipping_imethod1'] != '') {
	
		$strLocation = "";
HERE>>>	foreach($_POST['shipping_ilocation1'] as $nam=>$val) {
			$strLocation .= "'" . $val . "'" . ",";
		}	
		if ($strLocation != '') {
			$strLocation = substr($strLocation,0,strlen($strLocation)-1);
		}
		
		$sql_arr_ins = array('products_id' => $products_id,
							 'shipping_type' => 'International',
							 'shipping_service' => $_POST['shipping_imethod1'],
							 'shipping_cost' => $_POST['shipping_icost1'],
							 'shipping_add_cost' => $_POST['shipping_iadd_cost1'],
							 'shipping_location' => $strLocation);	
		zen_db_perform("product_ebay_shipping_methods", $sql_arr_ins);
	} 

Can anyone see anything obviously wrong with the coding, or can anyone think what's maybe causing it?

3 Jan 2013, 7:35 PM
#2
lhungil avatar

lhungil

Totally Zenned

Join Date:
Feb 2012
Location:
mostly harmless
Posts:
1,818
Plugin Contributions:
4

Re: PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

You are not checking to see if $_POST['shipping_ilocation1') is an array before passing it to foreach.

Example:

	if (isset($_POST['shipping_imethod1']) && is_array($_POST['shipping_ilocation1')) {

Feel free insert your preference for the first check (just one to ensure the index exists - some checks do more than others): array_key_exists('shipping_imethod1', $_POST), isset($_POST['shipping_imethod1']), zen_not_null($_POST['shipping_imethod1']), $_POST['shipping_imethod1'] !== null, etc

3 Jan 2013, 7:43 PM
#3
limelites avatar

limelites

Totally Zenned

Join Date:
Jan 2009
Posts:
2,085
Plugin Contributions:
0

Re: PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

lhungil:

You are not checking to see if $_POST['shipping_ilocation1') is an array before passing it to foreach.

Example:

if (isset($_POST['shipping_imethod1']) && is_array($_POST['shipping_ilocation1')) {
> 
> Feel free insert your preference for the first check (just one to ensure the index exists - some checks do more than others): array_key_exists('shipping_imethod1', $_POST), isset($_POST['shipping_imethod1']), zen_not_null($_POST['shipping_imethod1']), $_POST['shipping_imethod1'] !== null, etc

I'm sorry, I'm not too good with PHP.  Do you mean I should replace this:
```php
	foreach($_POST['shipping_ilocation1'] as $nam=>$val) {

with this:

if (isset($_POST['shipping_imethod1']) && is_array($_POST['shipping_ilocation1')) {

???

??

3 Jan 2013, 8:18 PM
#4
lhungil avatar

lhungil

Totally Zenned

Join Date:
Feb 2012
Location:
mostly harmless
Posts:
1,818
Plugin Contributions:
4

Re: PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

Replace: ```php
if ($_POST['shipping_imethod1'] != '') {

With: ```php
if (isset($_POST['shipping_imethod1']) && is_array($_POST['shipping_ilocation1')) {
3 Jan 2013, 9:26 PM
#5
limelites avatar

limelites

Totally Zenned

Join Date:
Jan 2009
Posts:
2,085
Plugin Contributions:
0

Re: PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

lhungil:

Replace: ```php
if ($_POST['shipping_imethod1'] != '') {

> With: ```php
if (isset($_POST['shipping_imethod1']) && is_array($_POST['shipping_ilocation1')) {

Replace: ```php
if ($_POST['shipping_imethod1'] != '') {


With: 
```php
if (isset($_POST['shipping_imethod1']) && is_array($_POST['shipping_ilocation1')) {

.causes a syntax error (according to Dreamweaver).

3 Jan 2013, 9:29 PM
#6
limelites avatar

limelites

Totally Zenned

Join Date:
Jan 2009
Posts:
2,085
Plugin Contributions:
0

Re: PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

Should maybe be this?

if (isset($_POST['shipping_imethod1']) && is_array($_POST['shipping_ilocation1'])) {
3 Jan 2013, 9:32 PM
#7
limelites avatar

limelites

Totally Zenned

Join Date:
Jan 2009
Posts:
2,085
Plugin Contributions:
0

Re: PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

Well, just uploaded it and it seems to do the trick... no more myDEBUG-000000000000.log's when I update or add new products.... thank you!

4 Jan 2013, 8:21 PM
#8
lhungil avatar

lhungil

Totally Zenned

Join Date:
Feb 2012
Location:
mostly harmless
Posts:
1,818
Plugin Contributions:
4

Re: PHP Warning: Invalid argument supplied for foreach() in update_product.php on line 2

limelites:

Should maybe be this?
Ahh, the beauty of typo's :P