<?php
error_reporting(0);
@session_start();
include("config.php");

$typ='gallery';
$flag = $_REQUEST[flag];


/***********************************/
$id= $_POST[id];
if($flag=="del")
{
	$id= $_GET[id];	
	
	
	$query=sprintf("Delete from gallery where id ='%d'", $id);
	$conn->query($query);
	header("Location:add-gallery-img.php?flag=del");
	exit;
}
if($id=="")
{ 
    	
		 $qadd4=sprintf("INSERT INTO gallery set typ='%s'", $typ);
		 $conn->query($qadd4);
		  
		 $id = mysqli_insert_id($conn); 
		if($_FILES["img"]["name"]!="") 
		{
				 
			 $fileload = $_FILES["img"]["name"];
			
			 $file_ext = explode(".",$fileload);
			 $myattach="gallery_".$id.".".$file_ext[1];
			 
			$destpath=$uploadpath.$myattach;
				 
			$file1 = move_uploaded_file($_FILES["img"]["tmp_name"],$destpath); 
			$updatecategory=sprintf("UPDATE gallery SET img ='%s' WHERE id ='%d'", $myattach, $id); 
			$conn->query($updatecategory);
		}		
		 header("Location:add-gallery-img.php?flag=dn");
		 exit;
}
$id= $_POST[id];
if($id!="")
{
          $query_up=sprintf("UPDATE gallery SET  typ='$typ' WHERE id='%d' ",$id); 
		 $conn->query($query_up);
   
		if($_FILES["img"]["name"]!="") 
		{
				 
			 $fileload = $_FILES["img"]["name"];
			
			 $file_ext = explode(".",$fileload);
			 $myattach="gallery_".$id.".".$file_ext[1];
			 
			$destpath=$uploadpath.$myattach;
				 
			$file1 = move_uploaded_file($_FILES["img"]["tmp_name"],$destpath); 
			$updatecategory=sprintf("UPDATE gallery SET img ='%s' WHERE id ='%d'", $myattach, $id); 
			$conn->query($updatecategory);
		}		
		
	
		header("Location:add-gallery-img.php?flag=up");
		exit;
		
		}
?>
