code

<?php
// Load the database configuration file
include_once ‘config.php’;

if(isset($_POST[‘importSubmit’])){

// Allowed mime types
$csvMimes = array(‘text/x-comma-separated-values’, ‘text/comma-separated-values’, ‘application/octet-stream’, ‘application/vnd.ms-excel’, ‘application/x-csv’, ‘text/x-csv’, ‘text/csv’, ‘application/csv’, ‘application/excel’, ‘application/vnd.msexcel’, ‘text/plain’);

// Validate whether selected file is a CSV file
if(!empty($_FILES[‘file’][‘name’]) && in_array($_FILES[‘file’][‘type’], $csvMimes)){

// If the file is uploaded
if(is_uploaded_file($_FILES[‘file’][‘tmp_name’])){

// Open uploaded CSV file with read-only mode
$csvFile = fopen($_FILES[‘file’][‘tmp_name’], ‘r’);

// Skip the first line
fgetcsv($csvFile);

// Parse data from CSV file line by line
while(($line = fgetcsv($csvFile)) !== FALSE){
// Get row data
$employee_code = $line[0];
$name = $line[1];
$dept = strtoupper($line[2]);
$position = $line[3];
$email = $line[4];
$phone = $line[5];
$isHod = $line[6];

$password = md5(‘1234567’);

$deptNo = mysqli_fetch_assoc(mysqli_query($conn,”SELECT * FROM `department` where UPPER(name)=’$dept'”))[‘id’];

if(mysqli_num_rows(mysqli_query($conn,”SELECT * FROM `employees` WHERE `employee_code`=’$employee_code'”))>0)
{
echo $employee_code.” already inserted!<br>”;
}
else
{
if(mysqli_query($conn,”INSERT INTO `employees`(`employee_code`, `name`, `employee_pos`, `email`, `password`, `contact`, `ishod`, `dept`, `status`) VALUES (‘$employee_code’,’$name’,’$position’,’$email’,’$password’,’$phone’,’$isHod’,’$deptNo’,’1′)”))
{
echo $employee_code.” inserted——-!<br>”;
}
else
{
echo “Something went wrong——-<br>”;
}
}

}

// Close opened CSV file
fclose($csvFile);

$qstring = ‘?status=succ’;
}else{
$qstring = ‘?status=err’;
}
}else{
$qstring = ‘?status=invalid_file’;
}
}
?>

<html>
<head>

</head>
<body>
<form method=”POST” enctype=’multipart/form-data’>
<input type=”file” name=”file”>
<button type=”submit” name=”importSubmit”>Upload data</button>
</form>
</body>
</html>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top