I'm trying to import the data from an xlsx to my database, but I have a problem with the insert (read the excel data, that's all right, but do not insert them) this is the error message:
Catchable fatal error: Object of class Box\Spout\Reader\XLSX\Reader could not
be converted to string in C:\xampp\htdocs\subir_excel\uploadExcel.php on line
62
I leave my php file:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "estudiantes_tuto";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "conectado";
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;
// Incluir Spout library
require_once 'src/Spout/Autoloader/autoload.php';
if (!empty($_FILES['file']['name'])) {
$pathinfo = pathinfo($_FILES["file"]["name"]);
if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls')
&& $_FILES['file']['size'] > 0 ) {
// Temporary file name
$inputFileName = $_FILES['file']['tmp_name'];
// Read excel file by using ReadFactory object.
$reader = ReaderFactory::create(Type::XLSX);
// Open file
$reader->open($inputFileName);
$count = 1;
// Number of sheet in excel file
foreach ($reader->getSheetIterator() as $sheet) {
// Number of Rows in Excel sheet
foreach ($sheet->getRowIterator() as $row) {
// It reads data after header. In the my excel sheet,
// header is in the first row.
if ($count > 1) {
// Data of excel sheet
$data['nombre_es'] = $row[0];
$data['apellido_pa'] = $row[1];
$data['apellido_ma'] = $row[2];
$sql="INSERT INTO estudiantes VALUES('$reader')";
$resultado=mysqli_query($this->conn,$sql);
//print_r($data);
}
$count++;
}
}
// Close excel file
$reader->close();
} else {
echo "Please Select Valid Excel File";
}
} else {
echo "Please Select Excel File";
}
?>
Line 62 is this:
$sql="INSERT INTO estudiantes VALUES('$reader')";
$resultado=mysqli_query($this->conn,$sql);