I must be blind or tired. I can not find the source of the error The error is "Parse error: Syntax error, unexpected end of file" and references the last line number of my code. What am I missing. Also any general PHP tips for the code would also be welcomed. I am only 8 weeks into learning PHP through a college course
<?php include '../view/header.php'; ?>
<?php require('../model/database.php');?>
<?php
$email = filter_input(INPUT_POST, 'email');
if ($email == '')
{
$error = 'Email cannot be blank. Please enter a valid email.';
require_once('index.php');
exit();
}
$query = 'SELECT customerID, firstName, lastName, email
FROM customers
WHERE email = :email';
$statement = $db->prepare($query);
$statement->bindValue(':email', $email);
$statement->execute();
$customer = $statement->fetch();
$statement->closeCursor();
if ($customer['email'] != $email)
{
$error = 'No matching email. Please enter a valid email.';
require_once('index.php');
exit();
}
$query = 'SELECT productCode
FROM products
ORDER BY productCode';
$statement = $db->prepare($query);
$statement->execute();
$productCodes = $statement->fetchAll();
$statement->closeCursor();
?>
<main>
<h2>Register Product</h2>
<p></p>
<form action="product_registered.php" method="post" id="aligned">
<input type="hidden" name="customerID"
value="<?php echo $customer['customerID']; ?>">
<label>Customer:</label>
<label><?php echo htmlspecialchars($customer['firstName'] . ' ' . $customer['lastName']); ?></label>
<br>
<label>Product:</label>
<select name="productCode">
<?php foreach ($productCodes as $productCode): ?>
<option value="<?php echo htmlspecialchars($productCode['productCode']); ?>">
<?php echo htmlspecialchars($productCode['name']); ?>
</option>
</select>
<br>
<label> </label>
<input type="submit" value="Register Product" /><br>
</form>
</main>
<?php include '../view/footer.php'; ?>
CodePudding user response:
There are two issues in your code.
(1) You have <?php foreach ($productCodes as $productCode): ?>
, but you are missing endforeach;
So, add the following line after the line </option>
<?php endforeach; ?>
(2) Did you fetch name
data ? Since you want to use $productCode['name']
in the following line
<?php echo htmlspecialchars($productCode['name']); ?>
So change the line:
$query = 'SELECT productCode FROM products ORDER BY productCode';
to
$query = 'SELECT productCode, name FROM products ORDER BY productCode';