php - CSV 上传无法正常工作

标签 php mysql codeigniter

在此,我想上传 csv 文件。验证不正确,但未经验证其工作。

这是我的 Controller :

public function uploadstatetype()
{
         $config['upload_path'] = APPPATH.'/assets/upload/';
         $config['allowed_types'] = 'csv';
        $config['max_size']  = '5000';
        with = ' ';
   $replace = '"';

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

if ( ! $this->upload->do_upload('file_name'))
{
    $error = array('error' => $this->upload->display_errors());
    $this->managestate($error);
}
else
{

//将文件信息插入数据库

    $data = array('upload_data' => $this->upload->data());
    $userfile = $data['upload_data']['file_name'];
    $this->load->library('csvreader');
    $upload_data = $this->upload->data(); 
    $this->load->library('csvreader');
    $file =  $upload_data['full_path'];
    $file_name =  $upload_data['userfile']; 

   $data = $this->csvreader->parse_file($file);

   foreach($data as $row)
   {

       if(($row['state_id']=='')||($row['state_name']==''))     
   {
     $this->session->set_flashdata('msg_excel','Please check the details in the file, some details are empty.');
    redirect(base_url().'admin/businesslocation/managestate');
 } 
 else
 {
     $results_array = array(
                       'state_id' => $row['state_id'],
                       'state_name' => $row['state_name'] 
                       );         
    $this->load->model('admin/businesslocation_model');
    $this->businesslocation_model->stateupload($results_array); 
    $success_message='Successfully Upload!';
    $this->session->set_flashdata('success_message',$success_message); 
    redirect(base_url().'admin/businesslocation/managestate');   

 }                        
}

} 
}

这是我的模型:

       function stateupload($results_array)
{

       $this->db->insert('cr_state', $results_array);



}

例如 state_id=1 和 state_name="kerala"。然后插入工作,当条件未检查时。但是当 if 条件给出时它不起作用。

最佳答案

像这样改变你的条件并尝试

   if(empty($row['state_id'])||(empty($row['state_name']))){

关于php - CSV 上传无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693956/

相关文章:

php - 如何在lighttpd中设置自定义php.ini位置?

c# - 如何在mysql和c#中的列表中选择子表的多条记录?

php - 按最近日期查询sql

Php,我应该使用 json 而不是 mysql 吗?

php - 如何获取MySQL数据库表的最后一个id,并将其存储在变量中

php - 在 codeigniter 中上传时压缩图像大小

php - 如何使用带有数据库中的值的复选框,其中数据库中的类型列(设置)codeigniter

php - mysqli::get_result 有什么问题?

PHP:非匿名函数的 use() 替代方法

php - joomla 安装新语言 DateTime::__construct(): 解析时间字符串失败 (jerror)