connect_error) { error_log("Database connection failed: " . ($conn->connect_error ?? "Connection not established")); header('HTTP/1.1 500 Internal Server Error'); echo json_encode(['error' => '資料庫連線失敗,請聯繫管理員']); ob_end_flush(); exit; } error_log("Database connection successful"); $conn->set_charset('utf8mb4'); // Initialize array to store query results $results = []; // Handle AJAX name search request if (isset($_GET['action']) && $_GET['action'] == 'search_name') { error_log("Processing AJAX name search"); header('Content-Type: application/json'); $search_term = isset($_GET['term']) ? trim($_GET['term']) : ''; if (strlen($search_term) > 16) { error_log("Search term too long: " . $search_term); echo json_encode(['error' => '搜尋詞過長']); ob_end_flush(); exit; } try { $sql = "SELECT Name FROM Member WHERE Name LIKE ? ORDER BY Name ASC"; $stmt = $conn->prepare($sql); if (!$stmt) { error_log("Prepare failed for name search: " . $conn->error); throw new Exception("Prepare failed: " . $conn->error); } $param = "{$search_term}%"; $stmt->bind_param('s', $param); if (!$stmt->execute()) { error_log("Execute failed for name search: " . $stmt->error); throw new Exception("Execute failed: " . $stmt->error); } $result = $stmt->get_result(); $names = []; while ($row = $result->fetch_assoc()) { $names[] = ['Name' => $row['Name']]; } echo json_encode($names); $result->free(); $stmt->close(); } catch (Exception $e) { error_log("Name search error: " . $e->getMessage()); header('HTTP/1.1 500 Internal Server Error'); echo json_encode(['error' => '查詢失敗,請聯繫管理員']); } ob_end_flush(); exit; } // Handle form submissions if ($_SERVER['REQUEST_METHOD'] === 'POST') { error_log("Processing POST request with action: " . ($_POST['action'] ?? 'none')); $action = $_POST['action'] ?? ''; $Dining_Date = $_POST['Dining_Date'] ?? ''; $Dining_Time = substr($_POST['Dining_Time'] ?? '', 0, 5); $Restaurant = substr($_POST['Restaurant'] ?? '', 0, 20); $Name = substr($_POST['Name'] ?? '', 0, 16); $Reason = substr($_POST['Reason'] ?? '', 0, 26); $Remarks = substr($_POST['Remarks'] ?? '', 0, 26); if (in_array($action, ['add', 'update', 'delete', 'query'])) { if ($action === 'add') { try { if ($Dining_Date && $Name && $Restaurant) { $stmt = $conn->prepare("INSERT INTO Warning_Log (Dining_Date, Dining_Time, Restaurant, Name, Reason, Remarks) VALUES (?, ?, ?, ?, ?, ?)"); if (!$stmt) { error_log("Prepare failed for INSERT: " . $conn->error); throw new Exception("Prepare failed: " . $conn->error); } $stmt->bind_param("ssssss", $Dining_Date, $Dining_Time, $Restaurant, $Name, $Reason, $Remarks); if (!$stmt->execute()) { error_log("Execute failed for INSERT: " . $stmt->error); throw new Exception("Execute failed: " . $stmt->error); } echo "

✅ 新增成功

"; } else { echo "

請填寫必要欄位:日期、名字、餐廳

"; } } catch (Exception $e) { error_log("Add error: " . $e->getMessage()); echo "

新增失敗: 請聯繫管理員

"; } } elseif ($action === 'update') { try { if (!$Dining_Date || !$Name) { echo "

請填寫必要欄位:日期、名字

"; ob_end_flush(); return; } $stmt = $conn->prepare("UPDATE Warning_Log SET Dining_Time = ?, Reason = ?, Remarks = ? WHERE Dining_Date = ? AND Restaurant = ? AND Name = ?"); if (!$stmt) { error_log("Prepare failed for UPDATE: " . $conn->error); throw new Exception("Prepare failed: " . $conn->error); } $stmt->bind_param("ssssss", $Dining_Time, $Reason, $Remarks, $Dining_Date, $Restaurant, $Name); if (!$stmt->execute()) { error_log("Execute failed for UPDATE: " . $stmt->error); throw new Exception("Execute failed: " . $stmt->error); } echo "

✅ 更新成功

"; } catch (Exception $e) { error_log("Update error: " . $e->getMessage()); echo "

更新失敗: 請聯繫管理員

"; } } elseif ($action === 'delete') { try { if (!$Dining_Date || !$Name) { echo "

請填寫必要欄位:日期、名字

"; ob_end_flush(); return; } $stmt = $conn->prepare("DELETE FROM Warning_Log WHERE Dining_Date = ? AND Restaurant = ? AND Name = ?"); if (!$stmt) { error_log("Prepare failed for DELETE: " . $conn->error); throw new Exception("Prepare failed: " . $conn->error); } $stmt->bind_param("sss", $Dining_Date, $Restaurant, $Name); if (!$stmt->execute()) { error_log("Execute failed for DELETE: " . $stmt->error); throw new Exception("Execute failed: " . $stmt->error); } echo "

✅ 刪除成功

"; } catch (Exception $e) { error_log("Delete error: " . $e->getMessage()); echo "

刪除失敗: 請聯繫管理員

"; } } elseif ($action === 'query') { try { $sql = "SELECT * FROM Warning_Log WHERE 1"; $params = []; $types = ''; if ($Dining_Date !== '') { $sql .= " AND Dining_Date = ?"; $params[] = $Dining_Date; $types .= 's'; } if ($Dining_Time !== '') { $sql .= " AND Dining_Time LIKE ?"; $params[] = "%$Dining_Time%"; $types .= 's'; } if ($Restaurant !== '') { $sql .= " AND Restaurant LIKE ?"; $params[] = "%$Restaurant%"; $types .= 's'; } if ($Name !== '') { $sql .= " AND Name = ?"; $params[] = $Name; $types .= 's'; } if ($Reason !== '') { $sql .= " AND Reason LIKE ?"; $params[] = "%$Reason%"; $types .= 's'; } if ($Remarks !== '') { $sql .= " AND Remarks LIKE ?"; $params[] = "%$Remarks%"; $types .= 's'; } $sql .= " ORDER BY Dining_Date DESC, Dining_Time DESC"; $stmt = $conn->prepare($sql); if (!$stmt) { error_log("Prepare failed for QUERY: " . $conn->error); throw new Exception("Prepare failed: " . $conn->error); } if (!empty($params)) { $stmt->bind_param($types, ...$params); } if (!$stmt->execute()) { error_log("Execute failed for QUERY: " . $stmt->error); throw new Exception("Execute failed: " . $stmt->error); } $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $results[] = $row; } $result->free(); $stmt->close(); } catch (Exception $e) { error_log("Query error: " . $e->getMessage()); echo "

查詢失敗: 請聯繫管理員

"; } } } } ?> Warning_Log 管理

警示記錄維護

日期: 時間: 餐廳:
名字:

原因:
備註:

筆符合條件

日期 時間 餐廳 原因 備註
close(); } ob_end_flush(); // End output buffering ?>