Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php insert_批处理代码点火器中的捕获错误_Php_Mysql_Codeigniter - Fatal编程技术网

Php insert_批处理代码点火器中的捕获错误

Php insert_批处理代码点火器中的捕获错误,php,mysql,codeigniter,Php,Mysql,Codeigniter,我混合了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 (respo

我混合了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;
}
假设多重插入失败,因为MySQL表中有唯一的列。我在firebug上遇到了以下错误:

发生数据库错误

错误号码:1062

钥匙“线圈号”重复输入“02NKTL216036109-2-1/8”

如何向普通用户显示这样的错误:“对不起,有问题,请检查您上传的CSV”


有可能吗?

您需要在PHP端使用TRY/CATCH,这样,如果数据不正确,您可以抛出错误,详细信息如下:


基本上,您需要尝试执行上载,如果数据不正确,它将向用户抛出一个“nice”错误…

您所要做的就是修改您使用的
插入批处理数据()
方法
插入批处理()
,如下所示

$this->db->db_debug = false;
// .....
 $insert = $this->db->insert_batch('table_name', $tryOne);
    if($insert > 0){
       $test = TRUE;
    }else{
        $test = FALSE;
    }
return $test;
现在在控制器
import\u from\u csv()

这将向ajax发送布尔值。或者,您可以根据自己的要求返回任何数据。所以技巧是使用
$this->DB->DB\u debug=false隐藏DB错误


您可以使用
config.php
隐藏所有数据库错误,其中
DB_debug
选项可用于将$this->DB->DB_debug=FALSE设置为
FALSE

Nice on$this->DB->DB_debug=FALSE。谢谢插入错误\u批处理ci将无法与TRY/CATCH一起使用
$this->db->db_debug = false;
// .....
 $insert = $this->db->insert_batch('table_name', $tryOne);
    if($insert > 0){
       $test = TRUE;
    }else{
        $test = FALSE;
    }
return $test;
echo json_encode($insert);