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!