<?php
include 'db.php';
header('Content-Type: application/json');

$id = intval($_POST['id']);   // 🔥 MISSING IN YOUR FILE

$category_id = intval($_POST['category_id']);
$subcategory_id = !empty($_POST['subcategory_id']) 
                    ? intval($_POST['subcategory_id']) 
                    : NULL;

$name = mysqli_real_escape_string($conn,$_POST['name']);
$unit = mysqli_real_escape_string($conn,$_POST['unit']);
$required = intval($_POST['required']);
$sort = intval($_POST['sort_order']);

$q = mysqli_query($conn,"
UPDATE variant_groups SET
category_id = $category_id,
subcategory_id = ".($subcategory_id !== NULL ? $subcategory_id : 'NULL').",
name = '$name',
unit = '$unit',
required = $required,
sort_order = $sort
WHERE id = $id
");

echo json_encode([
'status'=>$q?'success':'error',
'message'=>$q?'Variant updated successfully':'Update failed'
]);