简体   繁体   中英

import excel to mysql Error

I am trying to import data from an Excel file to Mysql, but it is not inserting the data

Controller

public function guardar_horario(){


 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['hrs_ini'] = $row[0];
                $data['hrs_ter'] = $row[1];
                $data['lunes'] = $row[2];
                $data['martes'] = $row[3];
                $data['miercoles'] = $row[4];
                $data['jueves'] = $row[5];
                $data['viernes'] = $row[6];
                $data['sabado'] = $row[7];



           $this->db->insert('horario',$row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7]);


           //$sql="INSERT INTO estudiantes (nombre_es, apellido_pa, apellido_ma) VALUES('$row[0]','$row[1]','$row[2]')";
           //$resultado=mysqli_query($conn,$sql);


                //print_r($data);

            }
            $count++;
        }
     }

     // Close excel file
     $reader->close();

    } else {

    echo "Please Select Valid Excel File";
  }

 } else {

 echo "Please Select Excel File";

 }
}
}

The error is

A Database Error Occurred

You must use the "set" method to update an entry.

Filename: C:/xampp/htdocs/SAE/system/database/DB_query_builder.php

Line Number: 1686

I think the syntax is wrong, I am new to codeigniter, and I am from Chile, I am sorry if it is not understood very well, but most of what I have found, has been in English, for them I ask the English speaking community.

You need to wrap your inserted params in an array, or at least that is one mistake:

$this->db->insert(
    'horario',
    array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7])
);

I think technically you would want to add your column names too, so you might have to do something like this:

$this->db->insert(
    'horario',
    array(
        'hrs_ini'   => $row[0],
        'hrs_ter'   => $row[1],
        'lunes'     => $row[2],
        'martes'    => $row[3],
        'miercoles' => $row[4],
        'jueves'    => $row[5],
        'viernes'   => $row[6],
        'sabado'    => $row[7]
    )
);

But depending on your data, this might be all you need:

$this->db->insert( 'horario', $data );

Your syntax is wrong. Please try this code and make sure in the array the column name should be same. For ex. 'hrs_ini' so in the db table the column name must be same as 'hrs_ini' otherwise data will not insert if any one of them is different or not found. Let me know is it working or not.

if($count > 1) { 
    //Make array here
    $data = array(
        'hrs_ini' => $row[0],
        'hrs_ter' => $row[1],
        'lunes' => $row[2],
        'martes' => $row[3],
        'miercoles' => $row[4],
        'jueves' => $row[5],
        'viernes' => $row[6],
        'sabado' => $row[7]
    ); 
    //Insert the data here
    $this->db->insert('horario',$data);
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM