<?php
$envFile = '.env';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $selectedDatabase = $_POST['database'];

    // Read the contents of the .env file
    $contents = file_get_contents($envFile);

    // Find the DB_DATABASE variable in the contents
    $pattern = "/^DB_DATABASE=.*/m";
    if (preg_match($pattern, $contents, $matches)) {
        $oldLine = $matches[0];

        // Replace the old line with the new value
        $newLine = "DB_DATABASE={$selectedDatabase}";
        $updatedContents = str_replace($oldLine, $newLine, $contents);

        // Write the updated contents back to the file
        if (file_put_contents($envFile, $updatedContents) !== false) {
            echo 'Variable value changed successfully.';
        } else {
            echo 'Unable to change variable value.';
        }
    } else {
        echo 'Variable not found in the .env file.';
    }
}