Question
I need help figuring out why I can't use my submit button to enter new information into my databse. I am using php to do
I need help figuring out why I can't use my submit button to enter new information into my databse. I am using php to do so. I have three files and am using MVC. I have three files. My index.php, my model\item_db.php and my view\add_product_form.php.
I am using a function called add_ToDoItems to run my sql query. For some reason it submits but then gets hung up somewhere and just give me a blank white screen on index.php. Can anyone give me some help to fix this? The query is correct and so is the submit form on add_product_form. I think the problem is on index.php with my controller but I am not sure why. I am guessing it is with the variables but again i am not sure.
Index.php:
require('model/database.php');
require('model/item_db.php');
require('model/category_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')
{
$category_id = filter_input(INPUT_GET, 'category_id',
FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == FALSE)
{
$category_id = 1;
}
$category_name = get_category_name($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
include('view/item_list.php');
}
else if ($action == 'delete_ToDoItem')
{
$itemNum = filter_input(INPUT_POST, 'itemNum',
FILTER_VALIDATE_INT);
$category_id = filter_input(INPUT_POST, 'category_id',
FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == FALSE ||
$itemNum == NULL || $itemNum == FALSE)
{
$error = "Missing or incorrect product id or category id.";
include('view/error.php');
}
else
{
delete_ToDoItem($itemNum);
header("Location: .?category_id=$category_id");
}
}
else if ($action == 'show_add_form')
{
$categories = get_categories();
include('view/add_product_form.php');
}
else if ($action == 'add_ToDoItem')
{
$category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
$title = $_POST['title'];
$description = filter_input(INPUT_POST, 'description', FILTER_UNSAFE_RAW);
if ($category_id == NULL || $category_id == FALSE || $title == NULL ||
$description == NULL)
{
$error = "Invalid product data. Check all fields and try again.";
include('error.php');
}
else
{
add_ToDoItem($category_id, $title, $description);
header("Location: .?category_id=$category_id");
}
}
else if ($action == 'list_categories')
{
$categories = get_categories();
include('view/category_list.php');
}
else if ($action == 'add_category')
{
$name = filter_input(INPUT_POST, 'name');
// Validate inputs
if ($name == NULL) {
$error = "Invalid category name. Check name and try again.";
include('view/error.php');
} else {
add_category($name);
header('Location: .?action=list_categories'); // display the Category List page
}
}
else if ($action == 'delete_category')
{
$category_id = filter_input(INPUT_POST, 'category_id',
FILTER_VALIDATE_INT);
delete_category($category_id);
header('Location: .?action=list_categories'); // display the Category List page
}
?>
add_product_form.php:
require('model/database.php');
$query = 'SELECT *
FROM categories
ORDER BY categoryID';
$statement = $db->prepare($query);
$statement->execute();
$categories = $statement->fetchAll();
$statement->closeCursor();
?>
ToDoList Manager
Add Category
View ToDoList
© ToDoList.
item_db.php:
function get_products_by_category($category_id) {
global $db;
$query = 'SELECT * FROM todoitems
WHERE todoitems.categoryID = :category_id
ORDER BY ItemNum';
$statement = $db->prepare($query);
$statement->bindValue(':category_id', $category_id);
$statement->execute();
$products = $statement->fetchAll();
$statement->closeCursor();
return $products;
}
function get_ToDoItem($itemNum) {
global $db;
$query = 'SELECT * FROM todoitems
WHERE ItemNum = :itemNum';
$statement = $db->prepare($query);
$statement->bindValue(':itemNum', $itemNum);
$statement->execute();
$product = $statement->fetch();
$statement->closeCursor();
return $product;
}
//delete ToDoItem
function delete_ToDoItem($itemNum) {
global $db;
$query = 'DELETE FROM todoitems
WHERE itemNum = :itemNum';
$statement = $db->prepare($query);
$statement->bindValue(':itemNum', $itemNum);
$statement->execute();
$statement->closeCursor();
}
function add_ToDoItem($category_id, $title, $description) {
global $db;
$query = 'INSERT INTO todoitems
(categoryID, Title, Description)
VALUES
(:category_id, :title, :description)';
$statement = $db->prepare($query);
$statement->bindValue(':category_id', $category_id);
$statement->bindValue(':title', $title);
$statement->bindValue(':description', $description);
$statement->execute();
$statement->closeCursor();
}
?>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started