<?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;
	 $prid = $post['prid'];
    $product = $post['product'];
   
     $name = '';
    $tmp_name = '';
    $file_url = '';
   
   /*$name2 = $_FILES['image2']['name'];  
 //Getting temporary file name stored in php tmp folder 
 $tmp_name2 = $_FILES['image2']['tmp_name'];
 //Path to store files on server
 $path = '../uploads/';
  if(!empty($name2)){
 //Moving file to temporary location to upload path 
 
 move_uploaded_file($tmp_name2,$path.$name2);
 
 //Displaying success message 
 //echo "Upload successfully";
 }else{
 //If file not selected displaying a message to choose a file 
 echo "Please choose a file";
 }
 
 
 $file_url2 =$path.$name2;*/
    
    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);
          
            //Displaying success message 
            //echo "Upload successfully";
        }
        $file_url = $path.$name;
        
    }
    
   
 
  
	if($post['id']==''){
    $query = "INSERT INTO productimages (`prid`,`product`,`image`) VALUES('$prid','$product','$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 productimages SET prid='".$prid."', product='".$product."'";
         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!');
        }

	}
}
?>