将数据从csv保存到php和mysql

时间:2014-11-29 08:28:26

标签: php mysql ajax csv

我正在尝试将所有数据从csv导入数据库。 下面的代码是发送到php文件的ajax请求。

$('#upload_location_csv').click(function(e){
    e.preventDefault();
    var queryDict = {};
    location.search.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]});
    fi = document.getElementById("csv_file_input").files[0];
    reader = new FileReader;
    reader.onload = function(){
        csv_data = reader.result;
        $.ajax({
            type:"post",
            url:ajax_root_path+'fend.php',
            data:{
                location_id:queryDict['id'],
                csv_data:csv_data,
                perform_action:"save_delivery_csv_data"
            },
            success:function(resObj){
                window.location.reload();
            },
            error:function(resObj){

            }

        });
    };
    reader.readAsText(fi);

});

这是执行的操作,此处为save_delivery_csv_data

当我点击导入csv时,页面重新加载,并且没有请求发送到fend.php。

case 'save_delivery_csv_data':
    {
        $location_id = $param_array['location_id'];
        $csv_data = $param_array['csv_data'];       
        $csv_data_rows  =   explode("\n",$csv_data);
        unset($csv_data_rows[0]);
        $csv_data_rows = array_values($csv_data_rows);
        debug($csv_data_rows);
        $csv_data_array = array();
        foreach ($csv_data_rows as $csv_row_key => $csv_row_value)
        {
            $csv_row_value_array    =   explode(",",$csv_row_value);
            debug($csv_row_value_array);
            if(count($csv_row_value_array) == 5)
            {   
                /*$csv_data_array[$csv_row_key] = array();
                $csv_data_array[$csv_row_key]['locality_id']    =   $csv_row_value_array[0];
                $csv_data_array[$csv_row_key]['locality_name']  =   $csv_row_value_array[1];
                $csv_data_array[$csv_row_key]['minimum_order_amount']   =   $csv_row_value_array[2];
                $csv_data_array[$csv_row_key]['delivery_time']  =   $csv_row_value_array[3];
                $csv_data_array[$csv_row_key]['delivery_charge']    =   $csv_row_value_array[4];*/
                //debug($csv_row_value_array);
                if(isset_empty($csv_row_value_array[3]))
                {
                    //debug($csv_row_value_array);

                    $locality_id =  $csv_row_value_array[0];


                    $location_outletValues = array();
                    $location_outletValues['cloud_site_id']         =   $cloud_site_id;
                    $location_outletValues['location_id']           =   $location_id;
                    $location_outletValues['locality_id']           =   $locality_id;
                    $location_outletValues['minimum_order']         =   $csv_row_value_array[2];
                    $location_outletValues['delivery_time']         =   $csv_row_value_array[3];
                    $location_outletValues['delivery_charge']       =   $csv_row_value_array[4];

                    $sql = "SELECT outlet_locality_id FROM ".PLATFORM_OUTLET_LOCALITIES."
                            WHERE location_id='".$location_id."' AND locality_id='".$locality_id."'";
                    $outlet_locality_array      =   $DB->query($sql);
                    //debug($outlet_locality_array);
                    if(count($outlet_locality_array) > 0)
                    {
                        $condition_array = array();
                        $condition_array['outlet_locality_id']  =   $outlet_locality_array[0]->outlet_locality_id;
                        //debug($condition_array);
                        debug($location_outletValues);
                        $DB->update(PLATFORM_OUTLET_LOCALITIES,$location_outletValues,$condition_array);
                    }
                    else
                    {
                        $DB->insert(PLATFORM_OUTLET_LOCALITIES,$location_outletValues);
                    }
                }


            }
        }
        break;
        //debug($csv_data_array);
    }

我错过了什么?我也尝试调试变量值,但没有请求发送到fend.php,而同一页面上的其他请求正在成功发送。

0 个答案:

没有答案