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 "
Error loading table '{$table}': " . $e->getMessage() . "
"; } return array(); } } // ดึงข้อมูลประเภทต่างๆ สำหรับตอนที่ 1 $prefixs = getTypeData('prefix', $pdo); $genders = getTypeData('typeSex', $pdo); $occupations = getTypeData('typeOccupation', $pdo); // ลองหาตารางสถานภาพ $maritalStatuses = getTypeData('typestatus', $pdo); if (empty($maritalStatuses)) { $maritalStatuses = getTypeData('typeStatus', $pdo); } if (empty($maritalStatuses)) { $maritalStatuses = getTypeData('type_status', $pdo); } if (empty($maritalStatuses)) { $maritalStatuses = getTypeData('marital_status', $pdo); } // ถ้าไม่พบตารางใดเลย ให้สร้างข้อมูลเริ่มต้น if (empty($maritalStatuses)) { $maritalStatuses = array( array('id' => 1, 'title' => 'โสด'), array('id' => 2, 'title' => 'สมรส'), array('id' => 3, 'title' => 'หย่าร้าง'), array('id' => 4, 'title' => 'แยกกันอยู่'), array('id' => 5, 'title' => 'หม้าย') ); } $serviceTypes = getTypeData('typeService', $pdo); $relationships = getTypeData('typeRelationship', $pdo); // ฟังก์ชันสำหรับดึงข้อมูล province function getProvinces($pdo) { try { $sql = "SELECT province_id as id, name_th as name FROM province ORDER BY name_th"; $stmt = $pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { if (isset($_GET['debug'])) { echo "
Error loading provinces: " . $e->getMessage() . "
"; } return array(); } } // ฟังก์ชันสำหรับดึงข้อมูล district พร้อม province_id function getDistricts($pdo) { try { $sql = "SELECT district_id as id, province_id, name_th as name FROM district ORDER BY name_th"; $stmt = $pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { if (isset($_GET['debug'])) { echo "
Error loading districts: " . $e->getMessage() . "
"; } return array(); } } // ฟังก์ชันสำหรับดึงข้อมูล subdistrict พร้อม district_id function getSubdistricts($pdo) { try { $sql = "SELECT subdistrict_id as id, district_id, name_th as name FROM subdistrict ORDER BY name_th"; $stmt = $pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { if (isset($_GET['debug'])) { echo "
Error loading subdistricts: " . $e->getMessage() . "
"; } return array(); } } // ดึงข้อมูลจังหวัด อำเภอ ตำบล $provinces = getProvinces($pdo); $districts = getDistricts($pdo); $subdistricts = getSubdistricts($pdo); // 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 "
"; echo "

เกิดข้อผิดพลาด:

"; echo "

ข้อความ: " . htmlspecialchars($e->getMessage()) . "

"; echo "

ไฟล์: " . htmlspecialchars($e->getFile()) . "

"; echo "

บรรทัด: " . $e->getLine() . "

"; echo "

Parameters: id=" . intval($id) . ", username=" . htmlspecialchars($username) . ", schoolID=" . htmlspecialchars($schoolID) . "

"; echo "
"; exit; } } ?>
ตอนที่ 1 - ข้อมูลส่วนตัว
>
onchange="toggleOtherInput('occupationOther', this.checked)">
ไม่พบข้อมูลสถานภาพในระบบ กรุณาติดต่อผู้ดูแลระบบ
>
กรุณากรอกเบอร์โทรศัพท์ 9-10 ตัวเลข รูปแบบถูกต้อง
กรุณากรอกอีเมล์ในรูปแบบที่ถูกต้อง รูปแบบถูกต้อง
ยกเลิก