<?php 
require_once '../config/db.php';
require_once '../config/database.class.php';
require_once '../config/functions.php';
$d = new database($db_config);


if(isset($_POST)){
	$post  =$_POST;
    $catid = $post['catid'];
     $product = str_replace("'","\'", $post['product']);
     $description = str_replace("'","\'", $post['description']);
   $name = '';
    $tmp_name = '';
    $file_url = '';
  
    
    if(empty($post['id']) && empty($_FILES['image']['name']))
    {
        //If file not selected displaying a message to choose a file while insert new products-------
        echo getResponse('error','Please choose a file');
        die;
    }
    else if(!empty($_FILES['image']['name']))
    {
        $name = $_FILES['image']['name'];
        $tmp_name = $_FILES['image']['tmp_name'];
        //Getting temporary file name stored in php tmp folder 
 
        //Path to store files on server
        $path = '../uploads/';
        if(!empty($name))
        {
            //Moving file to temporary location to upload path 
            move_uploaded_file($tmp_name,$path.$name);
            move_uploaded_file($tmp_name2,$path.$name2);
            //Displaying success message 
            //echo "Upload successfully";
        }
        $file_url = $path.$name;
       
    }
    
   
 
	if($post['id']==''){
    $query = "INSERT INTO subcategory(`catid`,`product`,`description`,`image`) VALUES('$catid','$product','$description','$file_url')";
        $executeQuery = $d->insertQuery($query);
        if($executeQuery){
        	echo getResponse('success','Data saved successfully');
        }else{
        	echo getResponse('error','Something went wrong, please try later!');
        }
	}else{
$id = $post['id'];
		$query = "UPDATE subcategory SET catid='".$catid."',product='".$product."',description='".$description."'";
		 if(!empty($name))
        {
            $query .= " , image='".$file_url."' ";
        }
        
         $query .= " WHERE id=$id";
        $executeQuery = $d->updateQuery($query);
        
        if($executeQuery){
        	echo getResponse('success','Data updated successfully');
        }else{
        	echo getResponse('error','Something went wrong, please try later!');
        }

	}
}


?>