<?php session_start();
require_once("../../config/config.php");
//require_once("../config/commonsetting.php");

$myformId=SESS_MASTER;
$myIp= ipCheck() ;
$dateceated=date("Y-m-d G:i:s");
$createdby='0';
$brand_key		=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'brand_key']);

$brand_name	=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'brand_name']);

$store_key		=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'store_key']);

$store_name	=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'store_name']);

$category_key		=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'category_key']);

$category_name	=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'category_name']);

$brand_name_ind= explode(',',$brand_name);
$store_name_ind= explode(',',$store_name);
$category_name_ind= explode(',',$category_name);


 if($_POST){
  try{
  	if(!empty($brand_key)){
     foreach($brand_name_ind as $data){
     	$sqlno = "SELECT coupon_code FROM  coupon WHERE coupon_code!='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryno=$conn->query($sqlno);
               $mydarano= $myqueryno->fetchAll()  ;
               
               $rows_foundno = $myqueryno->rowCount();
               
                
               $sqloff = "SELECT coupon_code FROM  coupon WHERE coupon_code ='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryoff=$conn->query($sqloff);
               $mydaraoff= $myqueryoff->fetchAll()  ;
               
               $rows_foundoff = $myqueryoff->rowCount();
          $olgaquery="INSERT INTO brand_master SET createdIp='$myIp',datecreated='$dateceated',brand_name='$data',brand_key='$brand_key',total_coupon ='$rows_foundno',total_offers ='$rows_foundoff'"; 
     
		$stmt = $conn->prepare($olgaquery);
		$stmt->execute();
			
			}
			}
				if(!empty($store_key)){
     foreach($store_name_ind as $data){
     	     
     	     $sqlno = "SELECT coupon_code FROM  coupon WHERE coupon_code!='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryno=$conn->query($sqlno);
               $mydarano= $myqueryno->fetchAll()  ;
               
               $rows_foundno = $myqueryno->rowCount();
               
                
               $sqloff = "SELECT coupon_code FROM  coupon WHERE coupon_code ='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryoff=$conn->query($sqloff);
               $mydaraoff= $myqueryoff->fetchAll()  ;
               
               $rows_foundoff = $myqueryoff->rowCount();

          $olgaquery="INSERT INTO store_master SET createdIp='$myIp',datecreated='$dateceated',store_name='$data',store_key='$store_key',total_coupon ='$rows_foundno',total_offers ='$rows_foundoff'"; 
     
		$stmt = $conn->prepare($olgaquery);
		$stmt->execute();
			
			}
			}
				if(!empty($category_key)){
     foreach($category_name_ind as $data){
     	$sqlno = "SELECT coupon_code FROM  coupon WHERE coupon_code!='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryno=$conn->query($sqlno);
               $mydarano= $myqueryno->fetchAll()  ;
               
               $rows_foundno = $myqueryno->rowCount();
               
                
               $sqloff = "SELECT coupon_code FROM  coupon WHERE coupon_code ='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryoff=$conn->query($sqloff);
               $mydaraoff= $myqueryoff->fetchAll()  ;
               
               $rows_foundoff = $myqueryoff->rowCount();
          $olgaquery="INSERT INTO category_master SET createdIp='$myIp',datecreated='$dateceated',category='$data',category_key='$category_key',total_coupon ='$rows_foundno',total_offers ='$rows_foundoff'"; 
     
		$stmt = $conn->prepare($olgaquery);
		$stmt->execute();
			
			}
			}
			 echo "<script>window.location='../brand-store-category.php';</script>";;
		
          
    
		
 
 
   }
         
        // show error
        catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        }
    }


?>