Errors: Invalid argument supplied for foreach() AND Undefined Variable

Hello!
I’m working on an assignment using MySQL/PHP/HTML.

I have 3 tables:

products
productid INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
make VARCHAR(50),
model VARCHAR(50),
stock INT(10)

providers
providerid INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(100)

users
userid INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
fname VARCHAR(50),
lname VARCHAR(50),
email VARCHAR(100),
phone VARCHAR(10)

From there, I had to create the following files:
menu.php to hold HTML for nav bar, inserted into each page
footer.php to hold HTML for footer info, inserted into each page
products.php to display a list of all products in the database
providers.php to display a list of all providers in the database
users.php to display a list of all users in the database
index.php as my controller

Here’s a snippet of PHP from index.php to show/populate the products.php page:

/* Start PRODUCTS.PHP page load */
	if (isset($_GET['viewproducts']))
	{
		include 'database.php';
		try
		{
			$sql = 'SELECT productid, make, model, stock FROM products';
			$result = $pdo->query($sql);
		}
		catch (PDOException $e)
		{
			$error = "Error fetching products: " . $e->getMessage();
			include 'error.html.php';
			exit();
		}
		foreach ($result as $row)
		{
			$products[] = array(
				'productid' => $row['productid'],
				'make' => $row['make'],
				'model' => $row['model'],
				'stock' => $row['stock']);
		}
		include 'products.php';
		exit();
	}
/* End PRODUCTS.PHP page load */

This is what I have in products.php:

<?php include_once 'helpers.inc.php';?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Products</title>
		<meta charset="utf-8">
	</head>
	<body>
		<header>
			<?php include 'menu.php';?>
		</header>

		<main>
			<p>Here are all of the products in the database:</p>
				<ul>
					<?php foreach ($products as $product): ?>
					<li>
						<form action="" method="post">
							<div>
								<strong>Make: </strong><?php htmlout($product['make']);?><br>
								<strong>Model: </strong><?php htmlout($product['model']);?><br>
								<strong>On hand: </strong><?php htmlout($product['stock']);?>
							</div>
						</form>
					</li>
					<p>==========</p>
					<?php endforeach;?>
				</ul>
		</main>

		<footer>
			<?php include 'footer.inc.html.php'; ?>
		</footer>
	</body>
</html>

When I navigate to the providers.php page, this is what I see:

Here are all of the providers in the database:

Notice: Undefined variable: provArray in C:\xampp\htdocs\dashboard\tests\week7\lab\providers.php on line 16

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\dashboard\tests\week7\lab\providers.php on line 16

It’s the same for my products.php and users.php pages.

What am I missing?

Thank you!

The script you postet does not contain any variable named provArray, so the error is somewhere else.

At least you could var_dump() the variables to have a look what’s in there. Maybe the variable is initialized within a condition that is not matched.

As above, you could use var_dump() or even echo() to trace through as the script runs.

Presuming that your first code snippet is not having problems connecting to the database, does it populate the $products array as you expect it to? If it does, is there something in helpers.inc.php that might be causing trouble? Does the $products array still exist after that include statement? If not, then there was.

Do the tables actually contain data? If not, or there is a chance that they will not, then your code will need to deal with that by checking whether each array exists before using it.

Hi @chorn and @droopsnoot,

I suppose it would help if I included the right snippets of code and error messages. That’s what I get for staring at it all day and posting right before bed! facepalm

When I navigate to the products page, this is what i get:

Notice: Undefined variable: products in C:\xampp\htdocs\dashboard\tests\week7\lab\products.php on line 16

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\dashboard\tests\week7\lab\products.php on line 16

The following is all that’s in helpers.inc.php

<?php
function html($text)
{
	return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}

function htmlout($text)
{
	echo html($text);
}
?>

The tables all contain data.

When I first started the assignment, I didn’t have a separate page for each table (products/provider/users), and the data populated with no problem.

I just tried removing the IF statement, and my index.php populated the list of products.

When I took the next step to break it apart and add the IF statements, it broke.

if (isset($_GET['viewproducts']))
{
}

Something about the three different IF statements is making it not work…

Thank you!!

That suggests that the condition you are checking for is not true, and because it’s not true, it does not populate the arrays, and therefore you get the error message.

@chorn & @droopsnoot

I found the error! Of course, it wasn’t where I was looking.

The ‘menu.php’ file (which contains my nav bar) that I included in ‘products.php’ contained the following:

<?php include_once 'helpers.inc.php';?>
	<nav>
		<p><a href="index.php">Home</a>&emsp;
			<a href="products.php" id="viewproducts">Products</a>&emsp;
			<a href="providers.php" id="viewproviders">Providers</a>&emsp;
			<a href="users.php" id="viewusers">Users</a>
		</p>
	</nav>

The problem was that I assumed "id=“viewproducts” would work with my “if (isset($_GET[‘viewproducts’]))” statement…

I was so wrong.

I removed the id completely, and changed it to “href=”?viewproducts" and it works!

Woo-hoo!

Thanks for looking everything over and helping me out :slight_smile:

1 Like

Glad it’s working now. That’s always the danger of only posting part of the code - it’s good to try to narrow it down and only post relevant bits, but if the problem comes from the code you didn’t post, it’s hard to narrow it down.

1 Like

@droopsnoot Agreed! I was trying to decide between what I thought was relevant and oversharing by providing all of the code. Next issue I run in to, I’ll include all of it. :slight_smile:

Thanks again!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.