﻿﻿??????????????
﻿﻿??????????????
<?php
include 'session.php';
include 'connection.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $id           = intval($_POST['id'] ?? 0);
    $student_name = trim($_POST['student_name'] ?? '');
    $admission_no = trim($_POST['admission_no'] ?? '');
    $roll_no      = trim($_POST['roll_no'] ?? '');
    $father_name  = trim($_POST['father_name'] ?? '');
    $course       = trim($_POST['course'] ?? '');
    $email        = trim($_POST['email'] ?? '');
    $mobile       = trim($_POST['mobile'] ?? '');
    $company_name = trim($_POST['company_name'] ?? '');
    $salary       = trim($_POST['salary'] ?? '');
    $location     = trim($_POST['location'] ?? '');
    $existingPhoto = trim($_POST['existing_photo'] ?? '');

    // Basic validation
    if ($id <= 0) {
        http_response_code(400);
        die("Invalid ID");
    }

    if (
        $student_name === '' || $admission_no === '' || $roll_no === '' ||
        $father_name === '' || $course === '' || $email === '' ||
        $mobile === '' || $company_name === '' || $salary === '' || $location === ''
    ) {
        http_response_code(400);
        die("All fields are required");
    }

    // ==========================
    // PHOTO HANDLING
    // ==========================
    $photoPath = $existingPhoto;

    if (isset($_FILES['photo']) && $_FILES['photo']['error'] === 0) {

        $uploadDir = "uploads/student_photos/";
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0777, true);
        }

        $newPhotoName = time() . '_' . basename($_FILES['photo']['name']);
        $newPhotoPath = $uploadDir . $newPhotoName;

        if (move_uploaded_file($_FILES['photo']['tmp_name'], $newPhotoPath)) {

            // Delete old photo if exists
            if (!empty($existingPhoto) && file_exists($existingPhoto)) {
                unlink($existingPhoto);
            }

            $photoPath = $newPhotoPath;
        }
    }

    // ==========================
    // UPDATE QUERY
    // ==========================
    $sql = "UPDATE placement_students SET
                student_name = ?,
                admission_no = ?,
                roll_no = ?,
                father_name = ?,
                course = ?,
                email = ?,
                mobile = ?,
                photo = ?,
                company_name = ?,
                salary = ?,
                location = ?
            WHERE id = ?";

    $stmt = $conn->prepare($sql);
    if (!$stmt) {
        http_response_code(500);
        die("Prepare failed: " . $conn->error);
    }

    $stmt->bind_param(
        "sssssssssssi",
        $student_name,
        $admission_no,
        $roll_no,
        $father_name,
        $course,
        $email,
        $mobile,
        $photoPath,
        $company_name,
        $salary,
        $location,
        $id
    );

    if ($stmt->execute()) {
        echo "OK";
        exit;
    } else {
        http_response_code(500);
        echo "Update failed: " . $stmt->error;
        exit;
    }
}

http_response_code(405);
echo "Invalid request";
?>