<?php
 //export.php
 include "../actions/dbPDO.php";
 if(!empty($_FILES["excel_file"]))
 {
      $file_array = explode(".", $_FILES["csv_file"]["name"]);
      if($file_array[1] == "csv")
      {
      $handle = fopen($_FILES["csv_file"]["tmp_name"], "r");
      $segl = 1 ;
   while($data = fgetcsv($handle))
   {
    $cust_name    =  $data[1];
    $nation       =  $data[2];
    $idSA         =  $data[3];
    $id_From      =  $data[4];
    $id_Expire    =  $data[5];
    $address      =  $data[6];
    $license      =  $data[7];
    $lic_from     =  $data[8];
    $lic_expire   =  $data[9];
    $phone        =  $data[10];
    $phone2       =  $data[11];
    $working_lic  =  $data[12];
    $w_phone      = $data[13];
    $working_woner =  $data[15];
    $working_address    =  $data[16];
    $w_phon2      =  $data[17];
    $dgree        =  $data[25];
    $comm         =  $data[26];
    $branchID     =  $data[27];
    $start        =  $data[28];

                  //  $check = $db->prepare("SELECT idSA , cust_name FROM customers WHERE idSA = '$idSA' OR cust_name = '$cust_name' LIMIT 1");
                  //  $check->execute();
                  // if($check->rowCount()>0){continue ; }

              $maxsql= $db->prepare("SELECT max(cust_id) as maxd FROM customers");
              $maxsql->execute();
              if($maxsql->rowCount()>0){$result = $maxsql->fetch(PDO::FETCH_ASSOC);
              $maxD = $result['maxd']+1;} else {$maxD = 1 ; }

                $sql = $db->prepare(" INSERT INTO customers(cust_id , cust_name, nation, idSA , id_From, id_Expire , address ,
                      license , lic_from , lic_expire , phone , phone2 , working_lic , w_phone , working_woner , working_address , w_phone2 ,
                    dgree , comm , branch , start )
                       VALUES (?, ?, ?, ?, ? , ?, ?,?, ?, ?, ?, ? , ?, ?,?, ?, ?, ?, ? , ?, ?)" );
                       $sql->bindParam(1,$maxD);
                       $sql->bindParam(2,$cust_name);
                       $sql->bindParam(3,$nation);
                       $sql->bindParam(4,$idSA);
                       $sql->bindParam(5,$id_From);
                       $sql->bindParam(6,$id_Expire);
                       $sql->bindParam(7,$address);
                       $sql->bindParam(8,$license);
                       $sql->bindParam(9,$lic_from);
                       $sql->bindParam(10,$lic_expire);
                       $sql->bindParam(11,$phone);
                       $sql->bindParam(12,$phone2);
                       $sql->bindParam(13,$working_lic);
                       $sql->bindParam(14,$w_phone);
                       $sql->bindParam(15,$working_woner);
                       $sql->bindParam(16,$working_address);
                       $sql->bindParam(17,$w_phone2);
                       $sql->bindParam(18,$dgree);
                       $sql->bindParam(19,$comm);
                       $sql->bindParam(20,$branch);
                       $sql->bindParam(21,$start);
                       $sql->execute() ;
                    $segl = $segl + 1 ;
                    }
                    fclose($handle);
 echo $segl ;
 }
 }

 ?>
