I am importing info from excel to a db table, and I wanted to ask how I could do a validation to avoid duplicate data, try this, but it gives me an error (HTTP ERROR 500) everything works for me and I import it but I can not do the validation to avoid duplicate info, example:
for ($indiceFila = 1; $indiceFila <= $numeroFilas; $indiceFila++) {
$valorA = $hojaActual->getCellByColumnAndRow(1, $indiceFila);
$valorB = $hojaActual->getCellByColumnAndRow(2, $indiceFila);
if (!empty($valorA)) {
$checkcod_duplicidad = "SELECT pr_nombre FROM productos WHERE pr_nombre='$valorA'";
$ca_dupli = mysqli_query($con, $checkcod_duplicidad);
$cant_duplicidad = mysqli_num_rows($ca_dupli);
if ($cant_duplicidad == 0) {
$sql = "INSERT INTO productos(pr_nombre,pr_cantidad) VALUES('$valorA','$valorB')";
$result = mysqli_query($conexion, $sql);
}
}
// $conexion->query($sql);
}
if ($result) {
unlink($archivo);
header("Location:form.php");
} else {
unlink($archivo);
header("Location:form.php");
}
PS : I am using phpSpreadSheet
Your problem with it repeating is probably because you don't use the
distinct
and this causes repeated values to be ignored, and it only displays it once.I hope it helps you, greetings.