<?php
include 'connect.php';
$speci_id=$_POST['speci_id'];
$speciName = $_POST['speciName'];
$speciCost = $_POST['speciCost'];
$speciPrice = $_POST['speciPrice'];
$speciQty = $_POST['speciQty'];

if (isset($_FILES['speciImage'])) {
    $imageName = $_FILES['speciImage']['name'];
    $imageTmp = $_FILES['speciImage']['tmp_name'];
    $imageExtension = pathinfo($imageName, PATHINFO_EXTENSION); // Get the file extension
    
    $folderName = "../images/Specifications/";
    $newImageName = uniqid() . '.' . $imageExtension;
    $destination = $folderName . $newImageName;
    $image = "images/Specifications/" . $newImageName;
    
    
    if (move_uploaded_file($imageTmp, $destination)) {
        // Update all fields
        $sql = "UPDATE a_specifications SET S_description = '$speciName', S_cost = $speciCost, S_price = $speciPrice,S_quantity=$speciQty,S_Image='$image' WHERE SpecificationID =$speci_id";
    } else {
        echo "Error uploading the image.";
    }
} else {
    // User hasn't changed the image, update only specific fields
    $sql = "UPDATE a_specifications SET S_description = '$speciName',S_cost = $speciCost,S_price=$speciPrice,S_quantity=$speciQty WHERE SpecificationID=$speci_id";
}
$result = mysqli_query($con, $sql);

if ($result) {
    echo "Category updated successfully.";
} else {
    echo "Error updating category: " . mysqli_error($con);
}
?>