duanji8615 2014-11-29 08:28
浏览 37

将数据从csv保存到php和mysql

I am trying to import all the data from csv to the database. Below code is ajax request that is sent to a php file.

$('#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);

});

This is the action that is performed, where here is, save_delivery_csv_data .

When I click on import csv, the page reloads and no request is sent to fend.php.

case 'save_delivery_csv_data':
    {
        $location_id = $param_array['location_id'];
        $csv_data = $param_array['csv_data'];       
        $csv_data_rows  =   explode("
",$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);
    }

What am I missing out here?? I also tried debugging variables values, but no request was sent to fend.php, whereas other request on the same page are being successfully sent.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作