1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
<?php
if(isset($_POST['Import'])) {
try {
$pdo_extraParams = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // rapport d'erreurs sous forme d'exceptions
PDO::ATTR_PERSISTENT => true, // Connexions persistantes
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" // encodage UTF-8
);
if(!isset($_FILES['file'])) throw new RuntimeException('Formulaire erronné');
if($_FILES['file']['error'] !== 0) throw new RuntimeException("Erreur à la réception du fichier");
$pdo = new PDO('mysql:host=localhost;dbname=gestion_compte;charset=utf8', 'root', '', $pdo_extraParams);
$pdo->beginTransaction();
$file = fopen($_FILES["file"]["tmp_name"], "r");
fgetcsv($file, 767, ";");
$stmt_select = $pdo->prepare('SELECT COUNT(*) FROM conge WHERE nom_dusage = :nom AND prenom = :prenom AND date_naissance = :naissance;');
$stmt_insert = $pdo->prepare('INSERT INTO conge(nom_dusage, prenom, nom_famille, date_naissance, categorie, grade) VALUES (:nom, :prenom, :famille, :naissance, :categorie, :grade);');
$stmt_update = $pdo->prepare('UPDATE conge SET categorie = :categorie AND grade = :grade WHERE nom_dusage = :nom AND prenom = :prenom AND date_naissance = :naissance;');
foreach([$stmt_select, $stmt_update, $stmt_insert] as $k => $stmt) {
$stmt->bindParam(':nom', $nom, PDO::PARAM_STR);
$stmt->bindParam(':prenom', $prenom, PDO::PARAM_STR);
$stmt->bindParam(':naissance', $naissance, PDO::PARAM_STR);
if($k == 0) continue;
elseif($k == 2)
$stmt->bindParam(':famille', $famille, PDO::PARAM_STR);
$stmt->bindParam(':categorie', $categorie, PDO::PARAM_STR);
$stmt->bindParam(':grade', $grade, PDO::PARAM_STR);
}
while (($getData = fgetcsv($file, 767, ";")) !== FALSE) {
list($nom, $prenom, $famille, $naissance, $categorie, $grade) = $getData;
$stmt_select->execute();
if($stmt_select->fetchColumn())
{
$stmt_update->execute();
echo "update fait";
}
else
{
$stmt_insert->execute();
echo "insert fait";
}
}
$pdo->commit();
fclose($file);
$msg = 'Succès';
}
catch(PDOException $ex) {
if(isset($pdo) and ($pdo instanceof PDO) and $pdo->inTransaction())
$pdo->rollBack();
error_log($ex->getMessage());
$msg = "Erreur dans la base de données";
}
catch(RuntimeException $ex) {
$msg = $ex->getMessage();
}
echo $msg;
} |
Partager