Good day!
I have code for uploading the files with the extension name .xml, after it was upload it will display in webpage and also save to database…
My problem is during on my testing …When I upload .xml file it did not upload, No error displayed…Sometimes when I upload it will upload, but theres a .xml file that did not upload, I cannot configured why it happens.
here is the code:
<?php
$data = array();
$con = mysql_connect("localhost", "root","");
if (!$con) {
die(mysql_error());
}
$db = mysql_select_db("hris", $con);
if (!$db) {
die(mysql_error());
}
$sql = "select * from attendance";
$result = mysql_query($sql, $con);
if (!$result) {
die(mysql_error());
}
$total = mysql_num_rows($result);
if ($total > 0) {
$sql = "delete from attendance";
$result = mysql_query($sql, $con);
if (!$result) {
die(mysql_error());
}
}
function add_employee($EMP_NO, $Lastname, $Firstname, $Middlename, $Date, $Time)
{
global $data;
$con = mysql_connect("localhost", "root","");
if (!$con){ die(mysql_error());}
$db = mysql_select_db("hris", $con);
if (!$db) {
die(mysql_error());
}
$EMP_NO = $EMP_NO;
$Lastname = $Lastname;
$Firstname = $Firstname;
$Middlename = $Middlename;
$Date = substr($Date,0,-13);
$Time = substr($Time,11,-4);
$Time = strftime('%I:%M %p', strtotime($Time));
$sql = "INSERT INTO attendance (EMP_NO, Lastname, Firstname, Middlename, Date, Time) VALUES ('$EMP_NO', '$Lastname', '$Firstname', '$Middlename', '$Date', '$Time')";
mysql_query($sql, $con);
$data []= array('EMP_NO' => $EMP_NO, 'Lastname' => $Lastname, 'Firstname' => $Firstname, 'Middlename' => $Middlename, 'Date' => $Date, 'Time' => $Time);
}
if ( $_FILES['file']['tmp_name'] )
{
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$rows = $dom->getElementsByTagName( 'Row' );
global $last_row;
$last_row = false;
$first_row = true;
foreach ($rows as $row)
{
if ( !$first_row )
{
$EMP_NO = "";
$Lastname = "";
$Firstname = "";
$Middlename = "";
$Date = "";
$Time = "";
$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell )
{
$ind = $cell->getAttribute( 'Index' );
if ( $ind != null ) $index = $ind;
if ( $index == 1 ) $EMP_NO = $cell->nodeValue;
if ( $index == 2 ) $Lastname = $cell->nodeValue;
if ( $index == 3 ) $Firstname = $cell->nodeValue;
if ( $index == 4 ) $Middlename = $cell->nodeValue;
if ( $index == 5 ) $Date = $cell->nodeValue;
if ( $index == 6 ) $Time = $cell->nodeValue;
$index += 1;
}
if ($EMP_NO=='' and $Lastname=='' and $Firstname=='' and $Middlename=='' and $Date=='' and $Time=='') {
$last_row = true;
}
else {
add_employee($EMP_NO, $Lastname, $Firstname, $Middlename, $Date, $Time);
}
}
if ($last_row==true) {
$first_row = true;
}
else {
$first_row = false;
}
}
}
?>
<html>
<body>
<table>
<tr>
<th>Employee Attendance</th>
</tr>
<?php foreach( $data as $row ) { ?>
<tr>
<td><?php echo( $row['EMP_NO'] ); ?></td>
<td><?php echo( $row['Lastname'] ); ?></td>
<td><?php echo( $row['Firstname'] ); ?></td>
<td><?php echo( $row['Middlename'] ); ?></td>
<td><?php echo( $row['Date'] ); ?></td>
<td><?php echo( $row['Time'] ); ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Thank you