Helllo All,
My PC is running PHP4, IIS connecting to a MSSQL SERVER.
I'm reading from a TXT file the rows that will be inserted into the database. The TXT file is created from a range of dates so I run my query at 10 AM it will insert the rows with no problem but if I run it again at 5 AM it will insert the rows I have already inserted and the new ones.
I thought I could make a query to check wich rows from the file are already inserted in the DB by checking the date and time for each row.
The problem now is that I run the load script once and It works just fine but if I run it again in order to see there are no duplicated insertions it never stops and makes the web server fail.
Hope you can help me.
This is my code:
<?php
// Abre el archivo solo para lectura
$fp = fopen("c:\\apache\htdocs\\mantenimiento\\DETALLE.TXT", "r");
if(!$fp)
{
echo "<font size=5 color=#00008b>Error</font><hr noshade><font size=3>El archivo no existe o no se encuentra en la ubicacion correcta<br></font>";
exit;
}
if($fp)
{
// Determina el nombre conque el sistema reconoce al archivo
$path = "c:\\apache\htdocs\\mantenimiento\\DETALLE.TXT";
$file = basename($path);
// Pasa los valore del archivo a un arreglo
$array = file ("c:\\apache\htdocs\\mantenimiento\\DETALLE.TXT");
//Consulta para determinar el numero deregistros y considerar el consecutivo
$consulta = "SELECT COUNT (*) FROM Incidencia";
$count = mssql_query($consulta);
while($fila = mssql_fetch_array($count))
{
$cve_inc = $fila[0];
}
// Consultas para determinar si esempleado o no
// Para cada posicion del arreglo
for ($i=0; $i<count($array); $i++)
{
// Clave incremental
$cve_inc = $cve_inc + 1;
// Manda la primer cadena a la variable
$cadena = $array[$i];
// Substrae la cadena para determinar el numero de empleado
$num_per = substr($cadena,4,5);
// Substrae la cadena para determinar la fecha
// Año
$year = substr($cadena,9,4);
// Mes
$month = substr($cadena,13,2);
// Dia
$day = substr($cadena,15,2);
// Agrega el caracter para el formato de fecha
$fecha = $month."/".$day."/".$year;
print "var fecha ";
print $fecha;
echo "<br>";
// Convierte la nueva cadena a fecha
$conv = strtotime($fecha);
// Y se le da formato
$date = date("Ymd", $conv);
print "var date ";
print $date;
echo "<br>";
// Calcula la hora
$hora = substr($cadena,17,2);
$minuto = substr($cadena,19,2);
$segundo = 00;
$times = $hora.":".$minuto.":".$segundo;
print "var times ";
print $times;
echo "<br>";
$tiempo = strtotime($times);
$time = date("H:i:s", $tiempo);
print "var time ";
print $time;
echo "<br>";
$var01 = substr($time,0,2);
$var02 = substr($time,3,2);
// Checar si ya existe una incidencia con la misma fechay hora para cada empleado
$select = "SELECT COUNT (*) FROM Incidencia WHERE Incidencia.num_per = '$num_per' AND Incidencia.fecha = '$fecha' AND Incidencia.hora = '$time'";
$qry_sel = mssql_query($select, $con_id);
while ($row = mssql_fetch_array($qry_sel))
{
$reg = $row[0];
print "var reg ";
print $reg;
echo "<br>";
}
if ($reg == 0)
{
// Checar si es Comida o Desayuno
if ( ($var01 >= 5) && ($var01 < 6) )
{
if($var02 >= 30)
{
// Inserta los datos en la Base de Datos
$tipo = "D";
//print "Desayuno mayor a 30";
$insert = "INSERT INTO Incidencia VALUES ('$cve_inc', '$num_per', '$date', '$time', '$tipo')";
$query = mssql_query($insert, $con_id) or die ("<h2><font size=5 color=#00008b>Error</font><hr noshade><font size=3>No fue posible insertar los datos importados<br></font>");
}
else
{
// Inserta los datos en la Base de Datos
$tipo = "C";
//print "Comida";
$insert = "INSERT INTO Incidencia VALUES ('$cve_inc', '$num_per', '$date', '$time', '$tipo')";
$query = mssql_query($insert, $con_id) or die ("<h2><font size=5 color=#00008b>Error</font><hr noshade><font size=3>No fue posible insertar los datos importados<br></font>");
}
}
else if ( ($var01 < 5) || ($var01 >= 10) )
{
$tipo = "C";
//print "Comida entre 10 am y 5 am";
// Inserta los datos en la Base de Datos
$insert = "INSERT INTO Incidencia VALUES ('$cve_inc', '$num_per', '$date', '$time', '$tipo')";
$query = mssql_query($insert, $con_id) or die ("<h2><font size=5 color=#00008b>Error</font><hr noshade><font size=3>No fue posible insertar los datos importados<br></font>");
}
else
{
// Inserta los datos en la Base de Datos
$tipo = "D";
//print "Desayuno menor a 30";
$insert = "INSERT INTO Incidencia VALUES ('$cve_inc', '$num_per', '$date', '$time', '$tipo')";
$query = mssql_query($insert, $con_id) or die ("<h2><font size=5 color=#00008b>Error</font><hr noshade><font size=3>No fue posible insertar los datos importados<br></font>");
}
} // End if reg
else{
}
} // End FOR
} // End If -- Chequeo de lectura
fclose($fp);
mssql_close($con_id);
echo "<font size=5 color=#00008b>Carga Completa</font><hr noshade><font size=3>Los datos fueron cargados exitosamente<br></font>";
// unlink("G:\apps\KRONOS\Comedor\DETALLE.TXT");
exit;
?>
😕