0

I am trying to import values from Excel spreadsheet using PHPSpreadsheet composer package. But it keep missing some rows from the excel file. what maybe the cause?

The code runs perfectly, and it inserts data in the mysql table.

let say i have 50 rows of data (apart from the title row), the query below will insert only 45 rows of the data.

Please view the code below: code:

$inputFileType = ''; //file extention

//check file extension
if(pathinfo($file, PATHINFO_EXTENSION) == 'csv'){
    $inputFileType = 'Csv';
} elseif(pathinfo($file, PATHINFO_EXTENSION) == 'xls'){
    $inputFileType = 'Xls';
} elseif(pathinfo($file, PATHINFO_EXTENSION) == 'xlsx'){
    $inputFileType = 'Xlsx';
}
//select file posted for inserting data
$inputFileName =  $_FILES["file"]["tmp_name"];

$reader = IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);

$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
for($row = 6; $row <= count($sheetData); $row++)
{
    $nr_orgUnit = $hospital;
    $inserted_by = $inserted_by;

    $xx = "'" . $nr_orgUnit . "', '" . implode("','", $sheetData[$row]) . "', '" . $inserted_by . "'";

    $stmt = $conn->query("INSERT INTO `ag_nacp_item_1_7`(`nr_orgUnit`, `PatientID`, `Age`, `Sex`, `NumberTimesStartedTBRX`, `Pregnant`, `KP`, `inserted_by`) VALUES($xx)");
}
if($stmt){
    echo '<div class="alert alert-success">
              <strong>Success!</strong> Report uploaded.
            </div>';
} else {
    echo '
    <div class="alert alert-warning">
        <strong>Error!</strong> ' . $sql . '<br>' . $conn->error .'
    </div>';
}
Emil Kitua
  • 59
  • 8

0 Answers0