PHP - Validar registros duplicados al subir CSV

 
Vista:
Imágen de perfil de Andersson

Validar registros duplicados al subir CSV

Publicado por Andersson (1 intervención) el 12/07/2017 16:28:00
Lo que necesito es poder subir un archivo .CSV, y si en la bd ya existe un registro con el mismo numero de PLANILLA y PROCESO duplicado que me salte un script que diga que ya existe un registro con ese numero de planilla y proceso. He realizado pruebas subiendo insertando datos directamente la db y trato de insertar los mismos datos me sale el error: "alert("Existe algun Registro Duplicado")" pero no me deja insertar dato. Hasta ahora el codigo que tengo es este:

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
extract($_POST);
if ($action == "upload") {
    $archivo = $_FILES['file']['tmp_name'];
    $row = 1 - 1;
    $fp = fopen ($archivo,"r");
 
    $PLANILLA = $_POST['PLANILLA'];
    $PROCESO = $_POST['PROCESO'];
 
    mysql_select_db($database_conexion, $conexion);
 
 
    $query_valida_planilla = "SELECT PLANILLA, PROCESO FROM huawei_acta_entrega WHERE PLANILLA = '$PLANILLA' AND PROCESO = '$PROCESO'";
    $valida_planilla = mysql_query($query_valida_planilla, $conexion) or die(mysql_error());
    $row_valida_planilla = mysql_fetch_assoc($valida_planilla);
    if(mysql_num_rows($valida_planilla)>0){
        echo '<script>alert("Ya existe un numero de planilla y proceso duplicado en la base de datos, Por favor revise el archivo");</script>';
    }else{
        //Comprovacion de datos:
        $verificado=false;
        while ($data = fgetcsv ($fp, 1000, ";")){
            $query_verificar_registro="SELECT PLANILLA, PROCESO FROM huawei_acta_entrega WHERE PLANILLA='".$data[1]."' AND PROCESO='".$data[3]."';";
            $verificar_registro = mysql_query($query_verificar_registro, $conexion) or die(mysql_error());
 
            if(mysql_num_rows($verificar_registro)>0){
                echo '<script>alert("Existe algun Registro Duplicado");</script>'; //VALIDA SI EXISTE ALGUN REGISTRO DUPLICADO
                $verificado=true;
                break;
 
            }
        }
        if($verificado==false){
            while ($data = fgetcsv ($fp, 1000, ";")){
                $num = count ($data);
                print "";
                $row++;
 
            $insertar="INSERT INTO huawei_acta_entrega (IDE, PLANILLA, N_PEDIDO, PROCESO, FECHA_E_FAC, HORA, FECHA_E_CON, FECHA_OPE, OBSERVACIONES) VALUES ('".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."', '".$data[5]."', '".$data[6]."', '".$data[7]."', '".$data[8]."');";
              mysql_query($insertar);
              //NO DEJA INSERTAR DATOS
            }
        }
        fclose ($fp);
 
     echo "<td><center><font face=\"arial\" color=\"green\"><b>Registros Insertados ".$row."</b></font></center></td>";
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder