Reposting again due to coding problem, I have attached the files
I am having trouble getting my button to submit the words in the text box over to the table on another page. What can I do to fix this?
Thanks
Chloe_6 0 Newbie Poster
<?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: .");
}
}
?>
<?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> </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'; ?>
<?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> </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'; ?>
<?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();
}
?>
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Thank you for resubmitting your code. It's much appreciated, to help us to help you :)
That being said, when you have:
<form action="product_list.php" method="post">
<input type="text" name="...">
<input type="submit">
</form>
Then, unless you have Javascript that is preventing the default event action, the values of the text input fields will be sent over in the POST request when you submit the form.
That being said, can you please explain in a little more detail exactly what you mean as far as submitting the words in the text box? Which form? Which text box?
In your file product_list.php, I see a form that posts to index.php to delete products in the table. Does that one work as intended?
In your file product_add.php, I see a form that posts to product_list.php. Is that the one you're having difficulty with?
Basically that form on the product_add.php page will submit a POST request to product_list.php with text input fields code, name, version, and release_date. The values will be user entered. The value of the submit button, itself, will be "Add Product" and there will be a hidden field with name action and value "add_product".
So now let's go to product_list.php, which is where we are POSTing to. I'm not seeing anything in the product_list.php file that inserts new records into the database.
In fact, I see that you have some code in index.php that says:
} else if ($action == 'add_product') { ... }
But I'm not seeing where that code block is ever being hit, because your form submits its data to product_list.php and not to index.php.
I do, see, however, that product_list.php loops through $products ... where is it getting this list of products? What is happening in header.php?? Is that where it's connecting to the database and pulling products from the database? Does index.php get called from within header.php? Can you upload header.php so we can see the full picture?
Chloe_6 0 Newbie Poster
I'm not sure if the delete button works as of yet. I am having trouble with the other button. It is suppose to take the text that is imputed into the text boxes and put that into the table on the product_list.php page. I'm having trouble with getting that data to go across. I'm only suppose to be using php and HTML for this
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
I'm only suppose to be using php and HTML for this
No worries. My comment about Javascript was that you can, theoretically, create Javascript that blocks normal HTML behavior. Provided you don't have Javascript, it's just normal HTML behavior.
Let's see how you structured your app:
index.php => This page seems to serve as the entry point into your app. It loads the database.php file (which you haven't included, but I assume it has the functions needed to connect to the database, as well as defining the $db variable), and it loads product_db.php, which is basically just a bunch of functions that do different things interacting with the database (and are either returning result sets, or adding a row to the products table).
I would clean up index.php and make it look like this instead:
<?php
require('../model/database.php');
require('../model/product_db.php');
// Try to retrieve $_POST['action']
$action = filter_input(INPUT_POST, 'action');
// If that didn't work ...
if ($action === NULL) {
// Try to retrieve $_GET['action']
$action = filter_input(INPUT_GET, 'action');
// If that didn't work
if ($action === NULL) {
$action = 'list_products';
}
}
if ($action == 'list_products') {
// Call the function defined in product_db.php to retrieve the list of products from the database
$products = get_products();
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Retrieve the product code we want to delete
$product_code = filter_input(INPUT_POST, 'product_code');
// If a product code was passed in via a form
if ($product_code != false) {
// Call the function defined in product_db.php to delete the product
delete_product($product_code);
}
// Retrieve the updated list of products, now that one has been deleted
$products = get_products();
// Show the updated product list
include('product_list.php');
} else if ($action == 'show_add_form') {
//Display the form to add a new product
include('product_add.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 {
// Call the function defined in product_db.php to add a new product record
add_product($code, $name, $version, $release_date);
// Retrieve the updated list of products, now that one has been added
$products = get_products();
// Show the updated product list
include('product_list.php');
}
}
?>
In product_add.php, try changing:
<form action="product_list.php" method="post" id="aligned">
into
<form action="index.php" method="post" id="aligned">
You see, the way you have it set up, index.php in your code is serving as the main page that the end-user interacts with, and everything branches off from there. Therefore, we want to submit the form to index.php.
Let me know how that works for you!
Chloe_6 0 Newbie Poster
This is working better than the code that I was using, but when I go to press the button I'm getting a blank page of the index.php instead of the product_list.php page
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Sometimes you get a blank page when there's a fatal PHP error (syntax error, parse error, etc.
Add this to the very top of index.php and it should spit out any errors:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Also, keep in mind, the code I typed out the other day was just typed here on DaniWeb in the reply textbox. I never checked it for errors.
Chloe_6 0 Newbie Poster
I have added in the code and done error checking and I'm still having the same result
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
So let me get this straight ... when you hit the button for the add_product form, you would expect it to redirect to index.php and then, on line 6, retrieve the action 'add_product'. Then, you would expect it to trigger line 50 of index.php where it retrieves the code, name, version, and release date from the form. Then, you would expect it to call add_product() as on line 66 of index.php, then retrieve the list of products as on line 69, and then show the updated product list as on line 72. Is that all correct?
What I would do is set up little debugging echo statements along the way to see where the breakdown is.
For example, on line 4 of index.php I would add:
echo "Loaded index.php successfully<br>";
Then on line 19 of index.php I would add:
echo "Determined the action is $action<br>";
Then on line 64 of index.php I would add:
echo "Ready to add the product to the database<br>";
The expectation would be for all 3 of those statements to print out. If none print out, and you can confirm by your web browser that you are correctly at index.php, then I would say you have a PHP fatal error occurring in database.php
Chloe_6 0 Newbie Poster
I'm needing the button to take the information that is put into the text boxes and go through the index.php file and output into the product_list.php file in the table that is supplied. From there, I should be able to delete the information that has been submitted onto that page
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Oh the button you're referring to is for when the action is delete_product?
This should be correct (the way you have it already):
<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>
What happens when you test out adding different echo statements to debug your code as in my above post?
Chloe_6 0 Newbie Poster
There's two buttons to this. The delete one and the add product button. I'm needing to get the add product button to take the information that is put into the text boxes and transfer it to the table on the product_list.php page, but it needs to go through the index.php page
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
I get that, and the changes I recommended for you should be doing that. If it's not working, I recommend adding debug echo statements.
Chloe_6 0 Newbie Poster
When I added in the echo statements, it shows all of the other coding underneath in the index.php. It doesn't redirect to the desired page
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
You don't have any redirects in your code? Do you mean the include('product_list.php') file is never opened? Add an echo statement right before and right after the include, and see if they both run.
Chloe_6 0 Newbie Poster
Yes I mean the include('product_list.php'). What should I be including in these echo statements?
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
The 3 echo statements I mentioned in my previous post, on the specific lines I mentioned.
Chloe_6 0 Newbie Poster
When I do that I get this showing up when I press any button.
"; ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); $action = filter_input(INPUT_POST, 'action'); if ($action === NULL) { $action = filter_input(INPUT_GET, 'action'); if ($action === NULL) { $action = 'list_products'; } } echo "Determined the action is $action
"; if ($action == 'list_products') { $products = get_products(); echo include('product_list.php'); echo } else if ($action == 'delete_product') { $product_code = filter_input(INPUT_POST, 'product_code'); if ($product_code != false) { delete_product($product_code); } $products = get_products(); include('product_list.php'); } else if ($action == 'show_add_form') { include('product_add.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); $products = get_products(); include("product_list.php"); } } ?> echo "Ready to add the product to the database
";
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
You have a syntax error. It looks like you didn't close a quote (") somewhere.
Chloe_6 0 Newbie Poster
I've attached the code cause I cannot seem to find the syntax error. I have all of my quotes closed.
<?php
require('../model/database.php');
require('../model/product_db.php');
echo "Loaded index.php successfully<br>";
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
$action = filter_input(INPUT_POST, 'action');
if ($action === NULL) {
$action = filter_input(INPUT_GET, 'action');
if ($action === NULL) {
$action = 'list_products';
}
}
echo "Determined the action is $action<br>";
if ($action == 'list_products') {
$products = get_products();
echo
include('product_list.php');
echo
} else if ($action == 'delete_product') {
$product_code = filter_input(INPUT_POST, 'product_code');
if ($product_code != false) {
delete_product($product_code);
}
$products = get_products();
include('product_list.php');
} else if ($action == 'show_add_form') {
include('product_add.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 {
echo "Ready to add the product to the database<br>";
add_product($code, $name, $version, $release_date);
$products = get_products();
include("product_list.php");
}
}
?>
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Move the ini_set() and error_reporting() function calls to right beneath the opening <?php and let me know what happens.
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Here is your syntax error right here:
echo
include('product_list.php');
echo
See, I told you there was definitely a syntax error :)
Chloe_6 0 Newbie Poster
I'm still getting the same thing coming up when I press a button
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Show me the updated code now.
Chloe_6 0 Newbie Poster
Here is the updated coding
<?php
require('../model/database.php');
require('../model/product_db.php');
echo "Loaded index.php successfully<br>";
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
$action = filter_input(INPUT_POST, 'action');
if ($action === NULL) {
$action = filter_input(INPUT_GET, 'action');
if ($action === NULL) {
$action = 'list_products';
}
}
echo "Determined the action is $action<br>";
if ($action == 'list_products') {
$products = get_products();
echo
include('product_list.php');
echo
} else if ($action == 'delete_product') {
$product_code = filter_input(INPUT_POST, 'product_code');
if ($product_code != false) {
delete_product($product_code);
}
$products = get_products();
echo
include('product_list.php');
echo
} else if ($action == 'show_add_form') {
echo
include('product_add.php');
echo
} 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.";
echo
include('../errors/error.php');
echo
} else {
echo "Ready to add the product to the database<br>";
add_product($code, $name, $version, $release_date);
$products = get_products();
echo
include('product_list.php');
echo
}
}
?>
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
You didn’t change either of the things I said to change. I’m in the car now nowhere near a computer. When I get home in a few hours, I can try to make the corrections for you and show you updated code. In the meantime, try applying the two code changes I said to make in my last few posts.
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Sorry, I forgot about you :)
You added all these statements that just say 'echo' but you need to complete the statement.
e.g.:
echo "String Here";
You can't just have
echo
on a line of its own, as you have multiple times in your code above.
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.