Hi guys/gals... currently working on more additions to my zencart website in the form of creating an application for mobile phones. All of this is kind of coming together real nice with a functioning api to fetch event data from my qr ticketing module and an api to fetch all products in json format ready for displaying in a mobile application, the idea is that the app will enable customers to view tickets, order food and browse our products and checkout all within the app. Today I was receiving artificial assistance when it produces a menuScreen.js for the react app its helping me build and i realise... wait, where are you fetching these "menu items" from. long story short i now have the following working admin page that seems to load to the admin dashboard when i try to add a menu category (table exists, sql queries from below verified as good)
Code:
<?php
require('includes/application_top.php');
require_once ('includes/defined_paths.php');
require_once ('includes/configure.php');
// Security check to ensure user is logged in as admin
if (!isset($_SESSION['admin_id'])) {
zen_redirect(zen_href_link(FILENAME_DEFAULT));
exit;
}
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Handle POST requests
if ($_POST) {
$action = $_POST['action'];
if ($action == 'add_category') {
$category_name = $_POST['category_name'];
$description = isset($_POST['description']) ? $_POST['description'] : '';
$status = isset($_POST['status']) ? 1 : 0;
if (!empty($category_name)) {
try {
$query = "INSERT INTO menu_categories (name, description, status) VALUES (:name, :description, :status)";
$stmt = $db->prepare($query);
$stmt->bindValue(':name', $category_name);
$stmt->bindValue(':description', $description);
$stmt->bindValue(':status', $status);
$stmt->execute();
echo "Category added successfully.";
} catch (PDOException $e) {
echo "Database error: " . $e->getMessage();
}
} else {
echo "Category name cannot be empty.";
}
} elseif ($action == 'add_menu_item') {
$name = $_POST['name'];
$description = $_POST['description'];
$price = (float)$_POST['price'];
$status = isset($_POST['status']) ? 1 : 0;
$category_id = (int)$_POST['category_id'];
if (!empty($name) && $price > 0) {
try {
$query = "INSERT INTO food_menu (name, description, price, status, category_id) VALUES (:name, :description, :price, :status, :category_id)";
$stmt = $db->prepare($query);
$stmt->bindValue(':name', $name);
$stmt->bindValue(':description', $description);
$stmt->bindValue(':price', $price);
$stmt->bindValue(':status', $status);
$stmt->bindValue(':category_id', $category_id);
$stmt->execute();
echo "Menu item added successfully.";
} catch (PDOException $e) {
echo "Database error: " . $e->getMessage();
}
} else {
echo "Name and price are required.";
}
} elseif ($action == 'edit_menu_item') {
$id = (int)$_POST['id'];
$name = $_POST['name'];
$description = $_POST['description'];
$price = (float)$_POST['price'];
$status = isset($_POST['status']) ? 1 : 0;
$category_id = (int)$_POST['category_id'];
try {
$query = "UPDATE food_menu SET name = :name, description = :description, price = :price, status = :status, category_id = :category_id WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->bindValue(':name', $name);
$stmt->bindValue(':description', $description);
$stmt->bindValue(':price', $price);
$stmt->bindValue(':status', $status);
$stmt->bindValue(':category_id', $category_id);
$stmt->bindValue(':id', $id);
$stmt->execute();
echo "Menu item updated successfully.";
} catch (PDOException $e) {
echo "Database error: " . $e->getMessage();
}
} elseif ($action == 'delete_menu_item') {
$id = (int)$_POST['id'];
try {
$query = "DELETE FROM food_menu WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->bindValue(':id', $id);
$stmt->execute();
echo "Menu item deleted successfully.";
} catch (PDOException $e) {
echo "Database error: " . $e->getMessage();
}
}
}
// Fetch menu categories and items
try {
$categories = $db->Execute("SELECT * FROM menu_categories");
$menu_items = $db->Execute("SELECT * FROM food_menu");
} catch (Exception $e) {
echo "Error fetching data: " . $e->getMessage();
}
// Handle Edit Item Request
$edit_item = null;
if (isset($_GET['edit'])) {
$edit_id = (int)$_GET['edit'];
$stmt = $db->prepare("SELECT * FROM food_menu WHERE id = :id");
$stmt->bindValue(':id', $edit_id);
$stmt->execute();
$edit_item = $stmt->fetch(PDO::FETCH_ASSOC);
}
require('includes/application_bottom.php'); // Finalize the script
?>
<!DOCTYPE html>
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script>
function confirmDelete() {
return confirm("Are you sure you want to delete this item?");
}
</script>
</head>
<body>
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<h1>Manage Menu Items</h1>
<h2>Add New Menu Item</h2>
<form action="menu_manager.php" method="post">
<input type="hidden" name="action" value="add_menu_item">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="description">Description:</label>
<textarea id="description" name="description"></textarea><br>
<label for="price">Price:</label>
<input type="text" id="price" name="price" required><br>
<label for="status">Status:</label>
<input type="checkbox" id="status" name="status" checked><br>
<label for="category_id">Category:</label>
<select id="category_id" name="category_id">
<?php while (!$categories->EOF): ?>
<option value="<?php echo $categories->fields['id']; ?>"><?php echo htmlspecialchars($categories->fields['name']); ?></option>
<?php $categories->MoveNext(); ?>
<?php endwhile; ?>
</select><br>
<input type="submit" value="Add Menu Item">
</form>
<?php if ($edit_item): ?>
<h2>Edit Menu Item</h2>
<form action="menu_manager.php" method="post">
<input type="hidden" name="action" value="edit_menu_item">
<input type="hidden" name="id" value="<?php echo (int)$edit_item['id']; ?>">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo htmlspecialchars($edit_item['name']); ?>" required><br>
<label for="description">Description:</label>
<textarea id="description" name="description"><?php echo htmlspecialchars($edit_item['description']); ?></textarea><br>
<label for="price">Price:</label>
<input type="text" id="price" name="price" value="<?php echo htmlspecialchars($edit_item['price']); ?>" required><br>
<label for="status">Status:</label>
<input type="checkbox" id="status" name="status" <?php echo $edit_item['status'] ? 'checked' : ''; ?>><br>
<label for="category_id">Category:</label>
<select id="category_id" name="category_id">
<?php while (!$categories->EOF): ?>
<option value="<?php echo $categories->fields['id']; ?>" <?php echo $categories->fields['id'] == $edit_item['category_id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($categories->fields['name']); ?>
</option>
<?php $categories->MoveNext(); ?>
<?php endwhile; ?>
</select><br>
<input type="submit" value="Update Menu Item">
</form>
<?php endif; ?>
<h2>Add New Category</h2>
<form action="menu_manager.php" method="post">
<input type="hidden" name="action" value="add_category">
<label for="category_name">Category Name:</label>
<input type="text" id="category_name" name="category_name" required><br>
<label for="description">Description:</label>
<textarea id="description" name="description"></textarea><br>
<label for="status">Status:</label>
<input type="checkbox" id="status" name="status" checked><br>
<input type="submit" value="Add Category">
</form>
<h2>Menu Items</h2>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Status</th>
<th>Category</th>
<th>Actions</th>
</tr>
<?php while (!$menu_items->EOF): ?>
<tr>
<td><?php echo htmlspecialchars($menu_items->fields['name']); ?></td>
<td><?php echo htmlspecialchars($menu_items->fields['description']); ?></td>
<td><?php echo htmlspecialchars($menu_items->fields['price']); ?></td>
<td><?php echo $menu_items->fields['status'] ? 'Active' : 'Inactive'; ?></td>
<td><?php echo htmlspecialchars($menu_items->fields['category_id']); ?></td>
<td>
<form action="menu_manager.php" method="get" style="display:inline;">
<input type="hidden" name="edit" value="<?php echo (int)$menu_items->fields['id']; ?>">
<input type="submit" value="Edit">
</form>
<form action="menu_manager.php" method="post" style="display:inline;" onsubmit="return confirmDelete();">
<input type="hidden" name="action" value="delete_menu_item">
<input type="hidden" name="id" value="<?php echo (int)$menu_items->fields['id']; ?>">
<input type="submit" value="Delete">
</form>
</td>
</tr>
<?php $menu_items->MoveNext(); ?>
<?php endwhile; ?>
</table>
<?php require('includes/application_bottom.php'); ?>
</body>
</html>
no error logs are being generated and when i re navigate to menu_manager.php i can clearly see that unfortunatly the dtabase query was not executed and the category was not inserted. I have tried reviewing other files on my site that run similar tasks but I am not seeing anything obvious that is wrong/missing myself. I also tried registering it as a page with admin page registration which lost me access to my admin panel for about half an hour 
Assuming I can get this working I can then return to react where i apparently need to edit the app.js slightly before testing to accomodate the described layout of buttons. End goal is to hopefully have something that works great and can easily be toned down to a standard zencart store too share. my products api for example, this has additional fields that would need to be removed and the app would not need a menu for food or events page for a standard release. Unsure how one would customise the look of the app either, i have not got to the stage of being ready to test just yet, graphical optimisation was last on the list of priorities lol.
Anyone free to possibly advise/help me work out what has gone wrong with the above? It literally looks fine and ready to go from the UI point of view in admin. if you wish to create the tables and give it a go on a test site ect, the sql queries required to install are...
Code:
CREATE TABLE food_menu (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
status TINYINT(1) DEFAULT 1,
category_id INT,
FOREIGN KEY (category_id) REFERENCES menu_categories(id)
);
and
Code:
CREATE TABLE menu_categories (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
status TINYINT(1) DEFAULT 1
);
My assumption is that either the database connection isn't actually being setup correctly on the query or the actual connection... or the location of the file (root of admin folder) is possibly causing some kind of permission issue?
I believe I would have to move this file eventually to make it sit inline with contribution guidelines for anyone wanting an app and menu functionality, main thing is to ensure it functions as expected first though. unless of course that is the issue lol
Bookmarks