<?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';

 
 
 $handle = fopen($_FILES['upload_data']['tmp_name'], "r");
 $headers = fgetcsv($handle, 100000, ",");
 $data = fgetcsv($handle, 100000, ",");
  $num = count($data);
    
   //  print_r($data);
     $sqlcondele = "SELECT * FROM coupon  WHERE from_site='Admitad'";
   
$myquerycondele=$conn->query($sqlcondele);
$mydaracon= $myquerycondele->fetchAll();

$rows_foundcodele = $myquerycondele->rowCount();
if( $rows_foundcodele>0){
  foreach( $mydaracon as $rowcondele ){
  
      $date_time  = $rowcondele['date_time'];
     $new_date = date('Y-m-d', strtotime($date_time));
      if($new_date!=date('Y-m-d')){
         
            $sqlde = "DELETE FROM coupon WHERE from_site='Admitad'";

    $myqde=$conn->query($sqlde);
          
      }else{
          echo 'failed';
         
      }
        
    }
   }
     
 while (($data = fgetcsv($handle, 100000, ","))!== FALSE) 
 {
    
 try{
     
   $company_name=addslashes($data[5]);
   $company_name1=strpos("$company_name", "[");
   if($company_name1==0){
    $company_name=addslashes($data[5]);
   }else{
    $company_name=substr_replace("$company_name","",$company_name1);
   }
   
   $code=addslashes($data[9]);
   $from_site='Admitad';
   $title=addslashes($data[1]);
   $description=addslashes($data[7]);
   $coupon_url=addslashes($data[11]);
   $category=addslashes($data[16]);
  $type=addslashes($data[15]);
  $exp_date=addslashes($data[13]);

   
  if($title!='name'){
	
   $olgaquery="INSERT INTO coupon SET company_name='$company_name',short_title ='$title',
       full_title ='$description',  link ='$coupon_url',category='$category',
 created_by='0',created_type='Admin',coupon_code ='$code',from_site='$from_site',date_time='$dateceated',  type ='$type',
 exp_date='$exp_date'"; 
 $stmt = $conn->prepare($olgaquery);
 if($stmt->execute()){
    $last_insert_id = $conn->lastInsertId();
    
       $status='success';
       $_SESSION["uploaded"] = $status ;
       

 }

  }
 
}

         
        // show error
        catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        }
     }
     
    
 echo '<br>';
 
fclose($handle);
 header("location:../fetch-data.php");