I have attached all of the coding needed
I'm using PHP to design a website. The problem that I am facing is that, the button on the add products page isn't taking the inputs and putting them on the products list page. I'm wondering what I can do to get the button to work correctly.

Here is the index.php page

<?php
require('../model/database.php');
require('../model/product_db.php');

$action = filter_input(INPUT_POST, 'action');
if ($action === NULL)
$action = filter_input(INPUT_GET, 'action');
if ($action === NULL)
$action = 'list_products';

if ($action == 'list_products')
// Get product data
$query= 'SELECT productCode, name, version, releaseDate FROM products WHERE productCode = :product_code;'
$statement = $db->prepare($query);
$statement->bindValue(':prodct_code', $product_code);
$statement->execute();
$products = $statement->fetchAll();
$statement->closeCursor();

$products = get_products();
// Display the product list
$query = 'SELECT * FROM products WHERE productCode = :product_code';
$statement = $db->prepare($query);
$statement->bindValue(':product_code', $product_code);
$statement->execute();
$product = $statement->fetch();
$statement->closeCursor();
include('product_list.php');

else if ($action == 'delete_product')
$product_code = filter_input(INPUT_POST, 'product_code');
if ($product_code != false)
$query = 'DELETE FROM products WHERE productCode = :product_code';
$statement = $db->prepare($query);
$statement->bindValue(':product_code', $product_code);
$success = $statement->execute();
$statement->closeCursor();

//Display the Product List page
include('product_list.php');
header("Location: .");
else if ($action == 'show_add_form')
// Add the product to the database
$query = 'INSERT INTO products (productCode, name, version, releaseDate) VALUES (:code, :name, :version, :release_date)';
$statement = $db->prepare($query);
$statement->bindValue(':code', $code);
$statement->bindValue(':name', $name);
$statement->bindValue(':version', $version);
$statement->bindValue(':release_date', $release_date);
$statement->execute();
$statement->closeCursor();

//Display the Product List page
include('product_list.php');

else if ($action == 'add_product')
$code = filter_input(INPUT_POST, 'code');
$name = filter_input(INPUT_POST, 'name');
$version = filter_input(INPUT_POST, 'version', FILTER_VALIDATE_FLOAT);
$release_date = filter_input(INPUT_POST, 'release_date');

// Validate the inputs
if ( $code === NULL || $name === FALSE ||
$version === NULL || $version === FALSE ||
$release_date === NULL)
$error = "Invalid product data. Check all fields and try again.";
include('../errors/error.php');
else
add_product($code, $name, $version, $release_date);
header("Location: .");

?>

the product_add.php page

<?php include '../view/header.php'?>
<main>
<h1>Add Product</h1>
<form action="product_list.php" method="post" id="aligned">
<input type="hidden" name="action" value="add_product">

<label>Code:</label>
<input type="text" name="code"><br>

<label>Name:</label>
<input type="text" name="name"><br>

<label>Version:</label>
<input type="text" name="version"><br>

<label>Release Date:</label>
<input type="text" name="release_date" />
<label class="message">Use 'yyyy-mm-dd' format</label><br>

<label>&nbsp;</label>
<input type="submit" value="Add Product" /><br>
</form>
<p><a href="product_list.php">View Product List</a></p>

</main>
<?php include '../view/footer.php'; ?>

the product_list.php page
<?php include '../view/header.php'?>
<main>
<h1>Product List</h1>
<!-- display a table of products -->
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th>Version</th>
<th>Release Date</th>
<th>&nbsp;</th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo htmlspecialchars($product['productCode']); ?></td>
<td><?php echo htmlspecialchars($product['name']); ?></td>
<td><?php echo htmlspecialchars($product['version']); ?></td>
<td><?php echo htmlspecialchars($product['releaseDate']); ?></td>
<td><form action="index.php" method="post">
<input type="hidden" name="action"
value="delete_product">
<input type="hidden" name="product_code"
value="<?php echo htmlspecialchars($product['productCode']); ?>">
<input type="submit" value="Delete">
</form></td>
</tr>
<?php endforeach; ?>
</table>
<p><a href="product_add.php">Add Product</a></p>

</main>
<?php include '../view/footer.php'; ?>

and the product_db.php page
<?php
function get_products()
global $db;
$query = 'SELECT * FROM products
ORDER BY name';
$statement = $db->prepare($query);
$statement->execute();
$products = $statement->fetchAll();
$statement->closeCursor();
return $products;

function get_products_by_customer($email)
global $db;
$query = 'SELECT products.productCode, products.name
FROM products
INNER JOIN registrations ON products.productCode = registrations.productCode
INNER JOIN customers ON registrations.customerID = customers.customerID
WHERE customers.email = :email';
$statement = $db->prepare($query);
$statement->bindValue(':email', $email);
$statement->execute();
$products = $statement->fetchAll();
$statement->closeCursor();
return $products;

function get_product($product_code)
global $db;
$query = 'SELECT * FROM products
WHERE productCode = :product_code';
$statement = $db->prepare($query);
$statement->bindValue(':product_code', $product_code);
$statement->execute();
$product = $statement->fetch();
$statement->closeCursor();
return $product;

function delete_product($product_code)
global $db;
$query = 'DELETE FROM products
WHERE productCode = :product_code';
$statement = $db->prepare($query);
$statement->bindValue(':product_code', $product_code);
$statement->execute();
$statement->closeCursor();

function add_product($code, $name, $version, $release_date)
global $db;
$query = 'INSERT INTO products
(productCode, name, version, releaseDate)
VALUES
(:code, :name, :version, :release_date)';
$statement = $db->prepare($query);
$statement->bindValue(':code', $code);
$statement->bindValue(':name', $name);
$statement->bindValue(':version', $version);
$statement->bindValue(':release_date', $release_date);
$statement->execute();
$statement->closeCursor();

function update_product($code, $name, $version, $release_date)
global $db;
$query = 'UPDATE products
SET name = :name,
version = :version,
releaseDate = :release_date
WHERE productCode = :product_code';
$statement = $db->prepare($query);
$statement->bindValue(':name', $name);
$statement->bindValue(':version', $version);
$statement->bindValue(':release_date', $release_date);
$statement->bindValue(':product_code', $code);
$statement->execute();
$statement->closeCursor();

?>

I've had to take out the curly brackets to be able to get the code to post.

Recommended Answers

All 2 Replies

I tried to format your code properly, but it's unreadable because it has no curly braces and none of it is indented.

We use the Markdown posting syntax here at DaniWeb, which means that each line of code needs to be indented by at least 4 spaces, with a blank line above and beneath code blocks, in order to post correctly. Please re-post your code by using the Code button in the editor toolbar. (The third button from the left). This will automatically indent your code properly for you.

More information is available at https://www.daniweb.com/welcome/syntax

Or attach it as a file?
PS. For the curious, this was a file Daniweb was sending me when I clicked on "Read" on the top bar. Guess I caught Daniweb when it was behaving oddly.

commented: Keep It Organized: Do not hijack someone else's topic with something completely unrelated -8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.