﻿﻿??????????????
﻿﻿??????????????
<?php
include 'session.php';
include 'connection.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Text fields
    $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'] ?? '');
    $existing_photo = trim($_POST['existing_photo'] ?? '');

    // Basic validation
    if (
        $student_name === '' || $admission_no === '' || $roll_no === '' ||
        $father_name === '' || $course === '' || $email === '' ||
        $mobile === '' || $company_name === '' || $salary === '' || $location === ''
    ) {
        die("All fields are required.");
    }

    // ==========================
    // PHOTO UPLOAD HANDLING
    // ==========================
    $photoPath = "";

    // Check if a new photo is uploaded
    if (isset($_FILES['photo']) && $_FILES['photo']['error'] === 0) {
        // Use ../uploads/ to store outside the admin folder
        $uploadDir = "../uploads/student_photos/";
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0777, true);
        }

        $photoName = time() . '_' . basename($_FILES['photo']['name']);
        $targetPath = $uploadDir . $photoName;

        if (move_uploaded_file($_FILES['photo']['tmp_name'], $targetPath)) {
            $photoPath = $targetPath;
        } else {
            die("Failed to upload photo.");
        }
    } elseif (!empty($existing_photo)) {
        // Use the existing photo path from the hidden input
        $photoPath = $existing_photo;
    } else {
        die("Photo is required. Please upload a photo or select a student with an existing photo.");
    }

    // ==========================
    // INSERT QUERY
    // ==========================
    $sql = "INSERT INTO placement_students 
            (student_name, admission_no, roll_no, father_name, course, email, mobile, photo, company_name, salary, location, status)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)";

    $stmt = $conn->prepare($sql);
    if (!$stmt) {
        die("Prepare failed: " . $conn->error);
    }

    $stmt->bind_param(
        "sssssssssss",
        $student_name,
        $admission_no,
        $roll_no,
        $father_name,
        $course,
        $email,
        $mobile,
        $photoPath,
        $company_name,
        $salary,
        $location
    );

    if ($stmt->execute()) {
        header("Location: placement.php?success=1");
        exit;
    } else {
        die("Insert failed: " . $stmt->error);
    }
}

die("Invalid request");
?>