could not be converted to string in foreach

0

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);
    
asked by Kvothe_0077 14.09.2017 в 21:35
source

1 answer

0

The query of the insert has an error,

Syntax:

$sql = "INSERT INTO TABLE (campo1, campo2, campo2)
VALUES ('datoainsertar1', 'datoainsertar12', 'datoainsertar13')";

Try with

 $sql="INSERT INTO estudiantes (nombre_es, apellido_pa, apellido_ma) VALUES('$row[0]','$row[1]','$row[2]')";
    
answered by 14.09.2017 / 22:23
source