<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
     <script src="/js/jquery-2.1.0.min.js"></script>
    
   
        </head>

<body>
    
   
    
    
    <?php
    
   
							
       

$row = 1;
if (($handle = fopen("uploads/".$_POST['fcsv'], "r")) !== FALSE) {
    $cd=0;
    for ($cd=0; $cd < 30; $cd++) { $data[$cd]="NULL" ;   }
    
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        
        	$servername1 = "localhost:8889";
								$username1 = "root";
								$password1 = "root";
								$dbname1 = "banque";

								// Create connection
								$conn1 = new mysqli($servername1, $username1, $password1, $dbname1);
								// Check connection
								if ($conn1->connect_error) {
								die("Connection failed: " . $conn1->connect_error);
								} 

        
         //*********************** Verifie si present ****************************************
        $sql1 = "SELECT * FROM `TABLE3` WHERE `COL 1`= '".$data[0]."'  ";
        print "-".$data[0]."-";
        $SOLDE=0;
        $PRESENT=0;        
        	$resultS = $conn1->query($sql1);
            if ($resultS->num_rows > 0) {
                while($row1 = $resultS->fetch_assoc()) {
                    $SOLDE=$SOLDE+floatval(str_replace(",", ".", $row1['COL 8']))*1;
                    echo " <strong>Déja présent</strong>";
                    $PRESENT=1; 
                     }
              //$RECHBANQUEQAL=str_replace(",", ".", $RECHBANQUE);
            //    $SOLDE=$RECHBANQUEQAL-$SOLDE;
              //  $PAIEMENT=$RECHBANQUEQAL-$SOLDE;
                 
            } 

          $conn1->close();	
        
        $num = count($data);
        echo "<p> $num champs à la ligne $row: <br /></p>\n";
        $row++;
         $str[0]=$data[0];
        
    print $PRESENT."//";
        if ($PRESENT==0) {
        for ($c=0; $c < $num; $c++) {
            $str[$c]=$data[$c];
            $str[$c]= mb_convert_encoding($str[$c], "UTF-8", "Windows-1252");
              $str[$c]=str_replace("'","\'",$str[$c]);
        
             echo $str[$c] . "<br />\n";
        }
            
            
            $servername1 = "localhost:8889";
								$username1 = "root";
								$password1 = "root";
								$dbname1 = "banque";

								// Create connection
								$conn1 = new mysqli($servername1, $username1, $password1, $dbname1);
								// Check connection
								if ($conn1->connect_error) {
								die("Connection failed: " . $conn1->connect_error);
								} 

        
            
            
             $sql1 = "INSERT INTO `TABLE3` (`COL 1`, `COL 2`, `COL 3`,`COL 4`, `COL 5`, `COL 6`,`COL 7`, `COL 8`, `COL 9`,`COL 10`, `COL 11`, `COL 12`,`COL 13`, `COL 14`, `COL 15`,`COL 16`, `COL 17`, `COL 18`,`COL 19`, `COL 20`, `COL 21`,`COL 22`, `COL 23`, `COL 24`,`COL 25`, `COL 26`, `COL 27`,`COL 28`, `COL 29`, `COL 30`)  VALUES (";
           $sql1=$sql1."'".$str[0]."','".$str[1]."','".$str[2]."','".$str[3]."','".$str[4]."','".$str[5]."','".$str[6]."','".$str[7]."','".$str[8]."','".$str[9]."','".$str[10]."','".$str[11]."','".$str[12]."','".$str[13]."','".$str[14]."','".$str[15]."','".$str[16]."','".$str[17]."','".$str[18]."','".$str[19]."','".$str[20]."','".$str[21]."','".$str[22]."','".$str[23]."','".$str[24]."','".$str[25]."','".$str[26]."','".$str[27]."','".$str[28]."',NULL) ";
            
            $resultSI = $conn1->query($sql1);
            
            print $sql1."<br>";
                  $conn1->close();	
  }
    
    }
    fclose($handle); 
}
     
    ?>
            
   </body>
</html>