First, thank you in advance for any help you all can provide :)
I am creating a website (add_product_form.php) that will prompt a user for information about a product in a form. It should then take that information and update a products table (add_product.php). However, when I click on the "Add Product" button it is not doing anything. It isn't throwing an error that I am seeing or anything.
I have done the following in trying to troubleshoot this myself, all to no avail: Research forms and form submissions on w3schools Compared my code to example code provided in Murach's PHP and MySQL 3rd edition
Here is the code for the add_product_form.php
<?php require('../model/database.php');
include '../view/header.php'; ?>
<main>
<h1>Add Product</h1>
<div id="aligned"
<form action="add_product.php" method="post" id="add_product_form">
<label>Code:</label>
<input type="text" name="code" placeholder="Enter Product Code"><br>
<label>Name:</label>
<input type="text" name="name" placeholder="Enter Product Name"><br>
<label>Version:</label>
<input type="text" name="version" placeholder="Enter Product Version"><br>
<label>Release Date:</label>
<input type="text" name="releaseDate" placeholder="Use 'yyyy-mm-dd' format"> Use 'yyyy-mm-dd' format<br>
<label> </label>
<input type="submit" value="Add Product"><br>
</form>
</div>
<p><a href="index.php">View Product List</a></p>
</main>
<?php include '../view/footer.php'; ?>
And here is the code from the add_product.php site:
<?php
$code = filter_input(INPUT_POST, 'code');
$name = filter_input(INPUT_POST, 'name');
$version = filter_input(INPUT_POST, 'version');
$releaseDate = filter_input(INPUT_POST, 'releaseDate');
// Validate inputs
if ($code == null || $code == false){
$error = "Invalid Code, please re-enter";
} elseif ($name == null || $name == false){
$error = "Invalid Name, please re-enter";
} elseif ($version == null || $version == false){
$error = "Invalid Version, please re-enter";
} elseif ($releaseDate == null || $releaseDate == false){
$error = "Invalid Release Date, please re-enter";
} else {
require_once('../model/database.php');
$query = 'INSERT INTO products
(code, name, version, releaseDate)
VALUES
(:code, :name:, :version, :releaseDate)';
$statement = $db->prepare($query);
$statement->bindValue(':code', $code);
$statement->bindValue(':name', $name);
$statement->bindValue(':version', $version);
$statement->bindValue(':releaseDate', $releaseDate);
$statement->execute();
$statement->closeCursor();
include('index.php');
}
if ($error != '') {
include('../errors/error.php');
exit();
}
?>
Again, thank you all for your help in advance :)
Read more here: https://stackoverflow.com/questions/65727515/why-is-my-form-not-submitting-data-to-update-my-database
Content Attribution
This content was originally published by Charles at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.