diff --git a/admin/orders.php b/admin/orders.php index dc62386..ec07f67 100644 --- a/admin/orders.php +++ b/admin/orders.php @@ -1,7 +1,7 @@ Access Denied.'; } else { - $stmt = $pdo->prepare("UPDATE tables SET table_number = ?, capacity = ?, area_id = ?, status = ? WHERE id = ?"); + $stmt = $pdo->prepare("UPDATE `tables` SET table_number = ?, capacity = ?, area_id = ?, status = ? WHERE id = ?"); $stmt->execute([$table_number, $capacity, $area_id, $status, $id]); $message = '
Table updated successfully!
'; } @@ -33,7 +33,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { if (!has_permission('tables_add')) { $message = '
Access Denied.
'; } else { - $stmt = $pdo->prepare("INSERT INTO tables (table_number, capacity, area_id, status) VALUES (?, ?, ?, ?)"); + $stmt = $pdo->prepare("INSERT INTO `tables` (table_number, capacity, area_id, status) VALUES (?, ?, ?, ?)"); $stmt->execute([$table_number, $capacity, $area_id, $status]); $message = '
Table created successfully!
'; } @@ -52,7 +52,7 @@ if (isset($_GET['delete'])) { try { $id = (int)$_GET['delete']; // Soft delete to avoid breaking historical order integrity - $pdo->prepare("UPDATE tables SET is_deleted = 1 WHERE id = ?")->execute([$id]); + $pdo->prepare("UPDATE `tables` SET is_deleted = 1 WHERE id = ?")->execute([$id]); header("Location: tables.php?deleted=1"); exit; } catch (PDOException $e) { @@ -67,11 +67,12 @@ if (isset($_GET['deleted'])) { $areas = $pdo->query("SELECT * FROM areas WHERE is_deleted = 0 ORDER BY name ASC")->fetchAll(); -$query = "SELECT `t`.`id`, `t`.`table_number`, `t`.`capacity`, `t`.`status`, `t`.`area_id`, `a`.`name` AS `area_name` - FROM `tables` AS `t` - LEFT JOIN `areas` AS `a` ON `t`.`area_id` = `a`.`id` - WHERE `t`.`is_deleted` = 0 - ORDER BY `a`.`name` ASC, `t`.`table_number` ASC"; +// Use a very standard query without backticks on aliases to maximize compatibility +$query = "SELECT t.id, t.table_number, t.capacity, t.status, t.area_id, a.name AS area_name + FROM `tables` t + LEFT JOIN areas a ON t.area_id = a.id + WHERE t.is_deleted = 0 + ORDER BY a.name ASC, t.table_number ASC"; $tables_pagination = paginate_query($pdo, $query); $tables = $tables_pagination['data']; diff --git a/api/order.php b/api/order.php index 15f0e7d..e2db1cb 100644 --- a/api/order.php +++ b/api/order.php @@ -34,11 +34,12 @@ try { $tid = $data['table_id'] ?? ($data['table_number'] ?? null); // Support both table_id and table_number as numeric ID if ($tid) { // Validate table exists AND belongs to the correct outlet + // Using standard aliases without backticks for better compatibility $stmt = $pdo->prepare( - "SELECT `t`.`id`, `t`.`table_number` - FROM `tables` AS `t` - JOIN `areas` AS `a` ON `t`.`area_id` = `a`.`id` - WHERE `t`.`id` = ? AND `a`.`outlet_id` = ?" + "SELECT t.id, t.table_number + FROM tables t + JOIN areas a ON t.area_id = a.id + WHERE t.id = ? AND a.outlet_id = ?" ); $stmt->execute([$tid, $outlet_id]); $table = $stmt->fetch(PDO::FETCH_ASSOC); @@ -52,10 +53,10 @@ try { if (!$table_id) { // Optional: try to find the first available table for this outlet $stmt = $pdo->prepare( - "SELECT `t`.`id`, `t`.`table_number` - FROM `tables` AS `t` - JOIN `areas` AS `a` ON `t`.`area_id` = `a`.`id` - WHERE `a`.`outlet_id` = ? + "SELECT t.id, t.table_number + FROM tables t + JOIN areas a ON t.area_id = a.id + WHERE a.outlet_id = ? LIMIT 1" ); $stmt->execute([$outlet_id]); @@ -419,4 +420,4 @@ You've earned *{points_earned} points* with this order. if ($pdo->inTransaction()) $pdo->rollBack(); error_log("Order Error: " . $e->getMessage()); echo json_encode(['success' => false, 'error' => $e->getMessage()]); -} \ No newline at end of file +} diff --git a/api/tables.php b/api/tables.php index d9e899e..93c84a1 100644 --- a/api/tables.php +++ b/api/tables.php @@ -10,12 +10,13 @@ try { $outlet_id = isset($_GET['outlet_id']) ? intval($_GET['outlet_id']) : 1; // Fetch all tables with their area names, filtered by outlet_id + // Using standard aliases without backticks for better compatibility $sql = " - SELECT `t`.`id`, `t`.`table_number` AS `name`, `t`.`capacity`, `a`.`name` AS `area_name`, `t`.`status` - FROM `tables` AS `t` - LEFT JOIN `areas` AS `a` ON `t`.`area_id` = `a`.`id` - WHERE `a`.`outlet_id` = :outlet_id AND `t`.`is_deleted` = 0 - ORDER BY `a`.`name` ASC, `t`.`table_number` ASC + SELECT t.id, t.table_number AS name, t.capacity, a.name AS area_name, t.status + FROM `tables` t + LEFT JOIN areas a ON t.area_id = a.id + WHERE a.outlet_id = :outlet_id AND t.is_deleted = 0 + ORDER BY a.name ASC, t.table_number ASC "; $stmt = $pdo->prepare($sql); $stmt->execute(['outlet_id' => $outlet_id]); diff --git a/includes/functions.php b/includes/functions.php index e28ce90..1391fc6 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -111,28 +111,28 @@ function paginate_query($pdo, $query, $params = [], $default_limit = 20) { } // Count total rows using a subquery to handle complex queries safely - // Strip ORDER BY from the query for the count to avoid SQL errors and improve performance - // Use a more robust regex that handles potential trailing semicolons or whitespace + // We wrap the original query into a subquery. This is the most reliable way. + // If ORDER BY is present, some older MySQL versions might fail in subquery, so we try to strip it. $count_query = preg_replace('/ORDER\s+BY.*?(?=;|$)/is', '', $query); $count_sql = "SELECT COUNT(*) FROM ($count_query) as count_table"; try { $stmt = $pdo->prepare($count_sql); $stmt->execute($params); - $total_rows = $stmt->fetchColumn(); + $total_rows = (int)$stmt->fetchColumn(); } catch (PDOException $e) { - // If stripping ORDER BY failed or caused issues, try with the original query in subquery + // Fallback to original query if regex stripping failed try { $count_sql_fallback = "SELECT COUNT(*) FROM ($query) as count_table"; $stmt = $pdo->prepare($count_sql_fallback); $stmt->execute($params); - $total_rows = $stmt->fetchColumn(); + $total_rows = (int)$stmt->fetchColumn(); } catch (PDOException $e2) { die("Pagination Count Error: " . $e2->getMessage() . "\nSQL: " . $count_sql); } } - $total_pages = ceil($total_rows / $limit); + $total_pages = $limit > 0 ? ceil($total_rows / $limit) : 1; if ($page > $total_pages && $total_pages > 0) $page = $total_pages; // Calculate offset @@ -159,6 +159,7 @@ function paginate_query($pdo, $query, $params = [], $default_limit = 20) { ]; } + /** * Render pagination controls and limit selector. * diff --git a/qorder.php b/qorder.php index 8aae575..d7b1210 100644 --- a/qorder.php +++ b/qorder.php @@ -1,7 +1,7 @@ prepare(" - SELECT `t`.`id`, `t`.`table_number` AS `table_name`, `a`.`outlet_id`, `o`.`name` AS `outlet_name` - FROM `tables` AS `t` - JOIN `areas` AS `a` ON `t`.`area_id` = `a`.`id` - JOIN `outlets` AS `o` ON `a`.`outlet_id` = `o`.`id` - WHERE `t`.`id` = ? + SELECT t.id, t.table_number AS table_name, a.outlet_id, o.name AS outlet_name + FROM `tables` t + JOIN areas a ON t.area_id = a.id + JOIN outlets o ON a.outlet_id = o.id + WHERE t.id = ? "); $stmt->execute([$table_id]); $table_info = $stmt->fetch(); @@ -526,4 +527,4 @@ foreach ($variants_raw as $v) { } - + \ No newline at end of file diff --git a/test_tables_logic.php b/test_tables_logic.php deleted file mode 100644 index 9a5f840..0000000 --- a/test_tables_logic.php +++ /dev/null @@ -1,36 +0,0 @@ - 1, - 'username' => 'admin', - 'group_name' => 'Admin', - 'permissions' => 'all' - ]; - - require_permission("tables_view"); - echo "Tables permission OK\n"; - - require_permission("orders_view"); - echo "Orders permission OK\n"; - - $pdo = db(); - $query = "SELECT t.*, a.name as area_name - FROM tables t - LEFT JOIN areas a ON t.area_id = a.id - WHERE t.is_deleted = 0 - ORDER BY a.name ASC, t.table_number ASC"; - $stmt = $pdo->query($query); - $tables = $stmt->fetchAll(); - echo "Tables query OK, found " . count($tables) . " tables\n"; - -} catch (Exception $e) { - echo "Caught exception: " . $e->getMessage() . "\n"; -} catch (Error $e) { - echo "Caught error: " . $e->getMessage() . "\n"; -} \ No newline at end of file