<?php
include "../../config/master.php";

$id = $_POST['id'];
$name = mysqli_real_escape_string($conn,$_POST['category_name']);

/* Get old image */
$old = mysqli_fetch_assoc(mysqli_query($conn,"SELECT image FROM gallery_categories WHERE id='$id'"));
$image = $old['image'];

/* If new image uploaded */
if(!empty($_FILES['image']['name'])){

$filename = time().'_'.$_FILES['image']['name'];

move_uploaded_file($_FILES['image']['tmp_name'],"../../uploads/gallery/".$filename);

/* delete old image (optional) */
if($image && file_exists("../../uploads/gallery/".$image)){
unlink("../../uploads/gallery/".$image);
}

$image = $filename;
}

/* Update */
mysqli_query($conn,"UPDATE gallery_categories SET category_name='$name', image='$image' WHERE id='$id'");

echo "success";