<?php
include "config.php";
require 'vendor/autoload.php'; // Include the Composer autoload file

use PhpOffice\PhpSpreadsheet\IOFactory;


// Handle file upload and data insertion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
    $file = $_FILES['file']['tmp_name'];

    // Load the spreadsheet
    $spreadsheet = IOFactory::load($file);
    $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);

    // Prepare your SQL statement
    $stmt = $conn->prepare("INSERT INTO ash (a, b) VALUES (?, ?)");

    foreach ($sheetData as $row) {
        // Skip the header row if there is one
        if ($row['A'] === 'A' && $row['B'] === 'B') {
            continue; // Skip header row
        }

        // Bind parameters (assuming your table has columns named 'a' and 'b')
        $stmt->bind_param("ss", $row['A'], $row['B']); // Adjust based on your Excel columns
        $stmt->execute();
    }

    $stmt->close();
    echo "Data has been successfully uploaded!";
}

$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Upload Excel File</title>
</head>
<body>
    <h1>Upload Excel File</h1>
    <form action="" method="post" enctype="multipart/form-data">
        <label for="file">Select Excel file:</label>
        <input type="file" name="file" id="file" accept=".xls,.xlsx" required>
        <button type="submit">Upload</button>
    </form>
</body>
</html>
