我正在尝试将数据从Excel文件导入Mysql,但它没有插入数据
控制器
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";
}
}
}
错误是
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
我觉得语法错了,我是codeigniter的新手,而且我来自智利,如果不是很了解我很抱歉,但我发现的大部分内容都是英文的,我问他们英语社区。 p>
答案 0 :(得分:0)
你需要将插入的参数包装在一个数组中,或者至少是一个错误:
$this->db->insert(
'horario',
array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7])
);
我认为从技术上讲你也想添加你的列名,所以你可能需要这样做:
$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]
)
);
但根据您的数据,这可能就是您所需要的:
$this->db->insert( 'horario', $data );
答案 1 :(得分:0)
你的语法错了。请尝试此代码,并确保在数组中列名称应相同。对于前者'hrs_ini'所以在db表中,列名必须与'hrs_ini'相同,否则如果其中任何一个不同或未找到,则不会插入数据。让我知道它是否有效。
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);
}