<?php
// Database connection
// Connect to MySQL

include_once '../dbconn.php';

if (isset($_POST["save"])) {

    $id = $_POST['id'];
    $submission_id = $_POST['submission_id'];


// Check if a file was uploaded without errors
    if (isset($_FILES["newCertificate"]) && $_FILES["newCertificate"]["error"] == 0) {
        $newFileName = '';

        // Get file details
        $fileName = $_FILES["newCertificate"]["name"];
        $fileTmpName = $_FILES["newCertificate"]["tmp_name"];
        $fileSize = $_FILES["newCertificate"]["size"];
        $fileError = $_FILES["newCertificate"]["error"];
        $fileType = $_FILES["newCertificate"]["type"];

        // Extract file extension
        $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
        // Generate a unique filename
        $unique_id = uniqid();
        $newFileName = "marriage-".  $submission_id . "." . $fileExtension;
         $newmarriagecert = 'https://visuretech.com/lacmon/uploads/'. "marriage-".  $submission_id . "." . $fileExtension;

        // Destination path to save the uploaded file
        $uploadPath = "../../uploads/" . $newFileName;

        // Move the uploaded file to the destination directory
        if (move_uploaded_file($fileTmpName, $uploadPath)) {
            
           
            //SQL query to update the database with the new image name
            $sql = "UPDATE submissions  SET Marriage_certificate_link = '$newmarriagecert' WHERE id = '$id'";

            if ($conn->query($sql) === TRUE) {
                 header("Location:../edit.php?id=$id saved");
                    exit();
            } else {
                echo "Error updating record: " . $conn->error;
            }

        } else {
            echo "Error uploading file!";
        }
    } else {
        echo "No file uploaded or error uploading file!";
    }

}
// Close connection
$conn->close();
?>
