【发布时间】:2016-09-03 17:20:00
【问题描述】:
我混合使用 ajax 和 codeigniter 来进行多次插入。
代码如下所示:
JS
function save(path){
$.ajax({
url: "<?php echo site_url('members/megumi/container_list/import_from_csv'); ?>",
type: 'post',
data: {path: path},
success: function (response) {
reload_table();
$('#modal_form').modal('hide'); // show bootstrap modal
}
});
return false;
}
PHP 代码点火器
public function import_from_csv(){
$csv = $this->input->post('path');
$tryOne = array();
if (file_exists($csv)) {
$file = fopen($csv, 'r'); // r flag is for readonly mode
while (( $line = fgetcsv($file) ) !== false) { // if line exists
$tryOne[] = $line; // add to array
}
fclose($file);
}
$tryOne = array_map(function($insert){
return array(
'CONSIGNEE' => $insert[1],
'CONTAINER' => $insert[2],
'SEAL' => $insert[3],
'THICK' => $insert[4],
'WIDTH' => $insert[5],
'SIZE' => $insert[6],
'COAT' => $insert[7],
'SPEC' => $insert[8],
'COIL_NO' => $insert[9],
'QTY' => $insert[10],
'PALET' => $insert[11],
'NET' => $insert[12],
'GROSS' => $insert[13],
'CONTRACT_NO' => $insert[14],
'TGL_TRANSFER' => $insert[15],
'LENGTH' => $insert[16],
'GRADE' => $insert[17],
'NO_URUT' => $insert[18]
);
}, $tryOne);
$insert = $this->container->insert_batch_data($tryOne);
echo json_encode($insert);
}
假设多次插入失败,因为我的 MySQL 表中有 UNIQUE 列。我在萤火虫上遇到了这个错误:
发生数据库错误
错误号:1062
密钥“COIL_NO”的重复条目“02NKTL216036109-2-1/8”
如何向普通用户显示这样的错误:“抱歉,出现问题,请检查您上传的 CSV”。
有可能吗?
【问题讨论】:
标签: php mysql codeigniter