CodeIgniter: загрузка данных из Excel intp существующей базы данных MySQL

я хочу загрузить данные в мою базу данных из Excel два раза (из отдельных файлов Excel)

Вот так мой стол:

id | name | address | account | note

и это первые данные Excel:

id | name | address

Второй Excel

account | note

Это мой контроллер:

public function upload(){
$this->load->helper('file');
$fileName = time().$_FILES['file']['name'];

$config['upload_path'] = FCPATH.'assets/'; //buat folder dengan nama assets di root folder
$config['file_name'] = $fileName;
$config['allowed_types'] = 'xls|xlsx|csv';
$config['max_size'] = 10000;

$this->load->library('upload');
$this->upload->initialize($config);

if(! $this->upload->do_upload('file') )
$this->upload->display_errors();

$media = $this->upload->data('file');

$inputFileName = $this->upload->data('full_path');
//'.assets/'.$media['file_name'];

try {
$inputFileType = IOFactory::identify($inputFileName);
$objReader = IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();

for ($row = 2; $row <= $highestRow; $row++){                  //  Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);$data= array(

"name"=> $rowData[0][1],
"address"=> $rowData[0][2]

);

$insert = $this->db->insert("info",$data);
delete_files($config['file_path'],TRUE);
}

}

Я пытался сделать тот же код для второго, но я не могу вставить данные в существующую строку, они всегда вставляются в новую строку.
Как это:

id | name | address | account | note
1     a       aa
2     b       bb
3                       1       a
4                       2       bb

Может кто-нибудь помочь / дать мне идею, как присоединиться к ним всем?
Любая помощь будет оценена! Спасибо!

2

Решение

Сделайте вторые данные Excel такими

id | account | note

и обновить account | note столбец с идентификатором

0

Другие решения

<?php

если (Исеть ($ _ POST [ «Импорт»])) {

    $filename=$_FILES["file"]["tmp_name"];if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
{$sql = "INSERT into table (name,address)
values ('".$getData[0]."','".$getData[1]."')";
$result = mysqli_query($con, $sql);
if(!isset($result))
{
echo "<script type=\"text/javascript\">
alert(\"Invalid File:Please Upload CSV File.\");
window.location = \"index.php\"</script>";
}
else {
echo "<script type=\"text/javascript\">
alert(\"CSV File has been successfully Imported.\");
window.location = \"index.php\"</script>";
}
}

fclose($file);
}
}

?>

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector