home/abhiramc/public_html/acts.service/import_employee.php 0000644 00000014757 15021466532 0020101 0 ustar 00 getConnection();
if (isset($_POST['submit'])) {
// Check if a file is uploaded
if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
$fileName = $_FILES['file']['tmp_name'];
$fileType = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
// Check if the uploaded file is a CSV
if ($fileType !== 'csv') {
echo "Please upload a CSV file.";
exit;
}
// Open the CSV file
if (($file = fopen($fileName, "r")) !== FALSE) {
// Skip the header row
fgetcsv($file);
$successCount = 0;
$errorCount = 0;
// Loop through each row
while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {
// Skip empty rows (where all columns are empty or contain only whitespaces)
if (empty(array_filter($column, fn($value) => !empty(trim($value))))) {
continue; // Skip this iteration and move to the next row
}
// Proceed with processing the row as before
$empbranchcode = !empty($column[0]) ? htmlspecialchars(strip_tags($column[0])) : null;
$loname = !empty($column[1]) ? htmlspecialchars(strip_tags($column[1])) : null;
$empadharnumber = !empty($column[2]) ? htmlspecialchars(strip_tags($column[2])) : null;
$empname = !empty($column[3]) ? htmlspecialchars(strip_tags($column[3])) : null;
$empfathername = !empty($column[4]) ? htmlspecialchars(strip_tags($column[4])) : null;
$empdob = !empty($column[5]) ? htmlspecialchars(strip_tags($column[5])) : null;
// Check if the empdob is valid and convert it to MySQL date format
if ($empdob) {
// Normalize the date: pad single-digit day and month with leading zero
$parts = explode('-', $empdob); // Use '-' as the separator now
if (count($parts) == 3) {
$day = str_pad($parts[0], 2, '0', STR_PAD_LEFT); // Pad single-digit day with leading zero
$month = str_pad($parts[1], 2, '0', STR_PAD_LEFT); // Pad single-digit month with leading zero
$year = $parts[2];
// Rebuild the normalized date string
$normalizedDate = $day . '-' . $month . '-' . $year;
} else {
$normalizedDate = null;
}
//echo "Normalized empdob: $normalizedDate
"; // Debugging line to check the normalized date
// Try to parse the normalized date using DateTime::createFromFormat
if ($normalizedDate) {
$date = DateTime::createFromFormat('d-m-Y', $normalizedDate); // Format: d-m-Y (with padded zeros)
// Check if the date was parsed correctly
if ($date === false) {
echo "Failed to parse date with format d-m-Y: $normalizedDate
";
$empdob = null; // Set to null if the date is invalid
} else {
// Check for any parsing errors with DateTime
$errors = DateTime::getLastErrors();
if ($errors['warning_count'] > 0) {
foreach ($errors['warnings'] as $error) {
echo "Warning: $error
";
}
}
$empdob = $date->format('Y-m-d'); // Convert to MySQL format (YYYY-MM-DD)
}
} else {
echo "Invalid date format for empdob: $empdob
";
$empdob = null; // Set to null if the date string could not be parsed
}
} else {
$empdob = null; // In case the empdob is empty, set it to null
}
$empqualification = !empty($column[6]) ? htmlspecialchars(strip_tags($column[6])) : null;
$empemail = !empty($column[7]) ? htmlspecialchars(strip_tags($column[7])) : null;
$empphone = !empty($column[8]) ? htmlspecialchars(strip_tags($column[8])) : null;
$empaddress = !empty($column[9]) ? htmlspecialchars(strip_tags($column[9])) : null;
$dgname = !empty($column[10]) ? htmlspecialchars(strip_tags($column[10])) : null;
$empbg = !empty($column[11]) ? htmlspecialchars(strip_tags($column[11])) : null;
$emppic = 'default-picture.png'; // Always set the default picture
$empstatus = 'active'; // Always set the status to Active
$emppass = !empty($column[14]) ? htmlspecialchars(strip_tags($column[14])) : null;
$doc_adhar = !empty($column[15]) ? htmlspecialchars(strip_tags($column[15])) : null;
$doc_polveri = !empty($column[16]) ? htmlspecialchars(strip_tags($column[16])) : null;
$doc_other = !empty($column[17]) ? htmlspecialchars(strip_tags($column[17])) : null;
// Prepare SQL query
$query = "INSERT INTO acts_emp (
empbranchcode, loname, empadharnumber, empname, empfathername, empdob, empqualification, empemail,
empphone, empaddress, dgname, empbg, emppic, empstatus, emppass, doc_adhar, doc_polveri, doc_other
) VALUES (
:empbranchcode, :loname, :empadharnumber, :empname, :empfathername, :empdob, :empqualification, :empemail,
:empphone, :empaddress, :dgname, :empbg, :emppic, :empstatus, :emppass, :doc_adhar, :doc_polveri, :doc_other
)";
$stmt = $db->prepare($query);
// Bind parameters
$stmt->bindParam(':empbranchcode', $empbranchcode);
$stmt->bindParam(':loname', $loname);
$stmt->bindParam(':empadharnumber', $empadharnumber);
$stmt->bindParam(':empname', $empname);
$stmt->bindParam(':empfathername', $empfathername);
$stmt->bindParam(':empdob', $empdob);
$stmt->bindParam(':empqualification', $empqualification);
$stmt->bindParam(':empemail', $empemail);
$stmt->bindParam(':empphone', $empphone);
$stmt->bindParam(':empaddress', $empaddress);
$stmt->bindParam(':dgname', $dgname);
$stmt->bindParam(':empbg', $empbg);
$stmt->bindParam(':emppic', $emppic);
$stmt->bindParam(':empstatus', $empstatus);
$stmt->bindParam(':emppass', $emppass);
$stmt->bindParam(':doc_adhar', $doc_adhar);
$stmt->bindParam(':doc_polveri', $doc_polveri);
$stmt->bindParam(':doc_other', $doc_other);
// Execute the query and check if successful
if ($stmt->execute()) {
//echo "Employee '$empname' imported successfully!
";
$successCount++;
} else {
//echo "Error importing '$empname'.
";
$errorCount++;
}
}
// After processing all rows, redirect to the target page with the result message
header("Location: https://abhiramcare.com/acts.service/dashboard.employee.view.php?status=success&successCount=$successCount&errorCount=$errorCount");
exit;
fclose($file);
} else {
echo "Unable to open the CSV file.";
}
} else {
echo "No file uploaded or there was an error.";
}
}
?>