alert('กรุณาเข้าสู่ระบบ'); window.location='../login.php';"; exit; } // เชื่อมต่อฐานข้อมูล include_once '../inc/connect.php'; include 'header.php'; // ฟังก์ชันสำหรับดึงข้อมูลจากตารางประเภทต่างๆ function getTypeData($table, $pdo) { try { $sql = "SELECT id, title FROM `{$table}` WHERE status = 1 ORDER BY id"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); // ถ้าไม่พบข้อมูลใน status = 1 ให้ลองดูทั้งหมด if (empty($result)) { $sql = "SELECT id, title FROM `{$table}` ORDER BY id"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); } return $result; } catch (PDOException $e) { // Debug: แสดง error ถ้ามีการ debug if (isset($_GET['debug'])) { echo "
";
echo "Subdistricts: " . count($subdistricts) . "\n";
echo "Districts: " . count($districts) . "\n";
echo "Provinces: " . count($provinces) . "\n";
if (count($subdistricts) > 0) {
echo "\nSample subdistrict: ";
print_r($subdistricts[0]);
}
echo "";
}
// Debug: ตรวจสอบข้อมูล marital status
if (isset($_GET['debug'])) {
echo "";
echo "Marital Statuses count: " . count($maritalStatuses) . "\n";
echo "Marital Statuses:\n";
print_r($maritalStatuses);
echo "\nAll type data loaded:\n";
echo "Prefixes: " . count($prefixs) . "\n";
echo "Genders: " . count($genders) . "\n";
echo "Occupations: " . count($occupations) . "\n";
echo "Service Types: " . count($serviceTypes) . "\n";
echo "Relationships: " . count($relationships) . "\n";
echo "";
}
// ตรวจสอบว่าเป็นการแก้ไขหรือเพิ่มใหม่
$isEdit = isset($_GET['mode']) && $_GET['mode'] == 'edit' && isset($_GET['id']);
$editData = array();
if ($isEdit) {
$id = intval($_GET['id']);
// ตรวจสอบความถูกต้องของ ID
if ($id <= 0) {
echo "";
exit;
}
try {
// ลองดึงข้อมูลแบบง่ายก่อน
$stmt = $pdo->prepare("SELECT * FROM individual_counseling WHERE id = ? AND username = ? AND schoolID = ?");
$stmt->execute(array($id, $username, $schoolID));
$editData = $stmt->fetch(PDO::FETCH_ASSOC);
// หากพบข้อมูล ให้ลองดึงชื่อ prefix เพิ่มเติม
if ($editData && !empty($editData['prefix_id'])) {
try {
$prefixStmt = $pdo->prepare("SELECT title FROM typePrefix WHERE id = ?");
$prefixStmt->execute(array($editData['prefix_id']));
$prefixData = $prefixStmt->fetch(PDO::FETCH_ASSOC);
if ($prefixData) {
$editData['prefix_name'] = $prefixData['title'];
}
} catch (PDOException $prefixError) {
// ถ้าไม่มีตาราง typePrefix หรือ error ก็ข้าม
$editData['prefix_name'] = null;
}
}
if (!$editData) {
echo "";
exit;
}
// ดึง birth_date แบบ raw (YYYY-MM-DD) เพื่อใช้กับ input type="date"
$bdStmt = $pdo->prepare("SELECT DATE_FORMAT(birth_date, '%Y/%m/%d') AS bd FROM individual_counseling WHERE id = ?");
$bdStmt->execute(array($id));
$bdRow = $bdStmt->fetch(PDO::FETCH_ASSOC);
$editData['birth_date_raw'] = ($bdRow && !empty($bdRow['bd'])) ? str_replace('/', '-', $bdRow['bd']) : '';
// Debug: แสดงข้อมูลที่ดึงมา
if (isset($_GET['debug'])) {
echo "";
echo "Edit Data:\n";
print_r($editData);
echo "\nPrefixes:\n";
print_r($prefixs);
echo "\nRelationships:\n";
print_r($relationships);
echo "\nSelected prefix_id: " . (isset($editData['prefix_id']) ? $editData['prefix_id'] : 'NULL') . "\n";
echo "Selected prefix_name: " . (isset($editData['prefix_name']) ? $editData['prefix_name'] : 'NULL') . "\n";
echo "Emergency relation ID: " . (isset($editData['emergency_relation_id']) ? $editData['emergency_relation_id'] : 'NULL') . "\n";
echo "Emergency relation other: " . (isset($editData['emergency_relation_other']) ? $editData['emergency_relation_other'] : 'NULL') . "\n";
echo "";
}
} catch (PDOException $e) {
// แสดง error แบบละเอียดเพื่อ debug
echo "ข้อความ: " . htmlspecialchars($e->getMessage()) . "
"; echo "ไฟล์: " . htmlspecialchars($e->getFile()) . "
"; echo "บรรทัด: " . $e->getLine() . "
"; echo "Parameters: id=" . intval($id) . ", username=" . htmlspecialchars($username) . ", schoolID=" . htmlspecialchars($schoolID) . "
"; echo "ข้อมูลส่วนตัวของผู้รับบริการ