Do not insert repeated data into MYSQL

0

I'm trying to upload some files to MySQL as a project, but the problem is that if I select the file 2 or more times it comes back and uploads them.

The idea is to make when you try to upload the file read the rows and say that X number of cells have already been uploaded, or if the entire file has already gone up then show it.

I am trying to do it for MYSQL, but I do not know if it is the most optimal way.

IF NOT EXISTS( SELECT carnet FROM Tabla_Carnet (NOLOCK) WHERE carnet = XXXXX )
BEGIN
        INSERT INTO Tabla_Carnet( carnet, fecha)
        VALUES( carnet, GETDATE() )
END

Here is what I'm inserting, therefore, are the same data from the XLSX file

$sql  = 'INSERT INTO Tabla_Carnet (                         fecha,
                                                            carnet,
                                                            hora,
                                                            nombre,
                                                            apellido,
                                                            sexo,
                                                            edad,
                                                            ocupacion,
                                                            estudios,
                                                            discapacidad)
                                        VALUES(             "'.$data.'",
                                                            "'.$hora.'",
                                                            "'.$data[2].'",
                                                            "'.$data[3].'",
                                                            "'.$data[6].'",
                                                            "'.$data[7].'",
                                                            "'.$data[8].'",
                                                            "'.$data[9].'",
                                                            "'.$data[10].'",
                                                          "'.$data[12].'")';

                $contador++;

Where the $ sql is executed

if ($data[7] != '') 
                    {
                    mysql_query($sql) or die(mysql_error());

                    if (!$sql)
                        {
                        echo '<div>¡Oh, detectamos un problema! 
                            <br>¡Por favor vuelva a intentarlo!</div>';
                        exit;
                        }
                    }
    
asked by FOX 31.08.2017 в 22:52
source

1 answer

2

Value Hash

If you find that you have to validate between two files, the best way to proceed would be to calculate the hash of that file and this value compare it with the ones you have stored.

Here is the documentation of how to generate the hash link

<?php
foreach(glob('/home/Kalle/myproject/*.php') as $ent)
{
    if(is_dir($ent))
    {
        continue;
    }

    echo $ent . ' (SHA1: ' . sha1_file($ent) . ')', PHP_EOL;
}
?>

After obtaining the hash of a file, you save it in an extra field, the next file you want to upload, you generate the hash and compare it against this field.

It will detect even the smallest change in a file.

    
answered by 12.09.2017 / 16:11
source