草庐IT

php - Codeigniter 将部分 CSV 数据插入 MYSQL 只有 id 和 date

coder 2023-10-20 原文

您好,我正在尝试通过上传 CSV 将数据插入 mysql 表。但我收到一个错误。上传显示错误并仅插入 ID 和日期。我不知道可能的原因。首先,我将用户表从 MYSQL 导出为 csv 格式。现在我使用我的代码上传相同但不起作用下面是我的代码:

我做了 Print_r($file_data); 所以得到这个

Array ( [file_name] => users_(2)3.csv [file_type] => text/plain [file_path] => /var/www/Test/uploads/ [full_path] => /var/www/Test/uploads/users_(2)3.csv [raw_name] => users_(2)3 [orig_name] => users_(2).csv [client_name] => users (2).csv [file_ext] => .csv [file_size] => 3.83 [is_image] => [image_width] => [image_height] => [image_type] => [image_size_str] => )

Controller :

    function importcsv() {
    $data['users'] = $this->csv_m->get_users();
    $data['error'] = '';    //initialize image upload error array to empty


    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'csv';
    $config['max_size'] = '1000';

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


    // If upload failed, display error
    if (!$this->upload->do_upload()) {
        $data['error'] = $this->upload->display_errors();
        print_r($_FILES);

        $this->load->view('csv', $data);
    } else {


        $file_data = $this->upload->data();
        $file_path =  './uploads/'.$file_data['file_name'];

        if ($this->csvimport->get_array($file_path))
         {
            $csv_array = $this->csvimport->get_array($file_path);

            foreach ($csv_array as $row)
             {
                $insert_data = array(

                    'id'=>$row['id'],
                    'sip_id'=>$row['sip_id'],
                    'sip_pass'=>$row['sip_pass'],
                    'key'=>$row['key'],
                    'name'=>$row['name'],
                    'status'=>$row['status'],
                    'email'=>$row['email'],
                    'password'=>$row['password'],
                    'phone'=>$row['phone'],
                    'balance'=>$row['balance'],
                    'created'=>$row['created'],
                    'modified'=>$row['modified'],
                    'date_inactive'=>$row['date_inactive'],
                    'reset_date'=>$row['reset_date'],


                );

                print_r($file_data);

                $this->csv_m->insert_csv($insert_data);
            }
            $this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
            redirect(base_url().'csv');
            //echo "<pre>"; print_r($insert_data);
        } else 
            $data['error'] = "Error occured";
            $this->load->view('csv', $data);
        }


    }

模型:

<?php

class Csv_m extends CI_Model {

function __construct() {
    parent::__construct();

}

function get_users() {     
    $query = $this->db->get('users');

    if ($query->num_rows() > 0)
     {
        return $query->result_array();
    } 
    else 
    {
        echo"Nothing To Show";
        return FALSE;
    }
}

function insert_csv($data) {
    $this->db->insert('users', $data);
}

View :

   <?php if (isset($error)): ?>
            <div class="alert alert-error"><?php echo $error; ?></div>
        <?php endif; ?>


        <h2>CI Addressbook Import</h2>
            <form method="post" action="<?php echo base_url() ?>csv/importcsv" enctype="multipart/form-data">
                <input type="file" name="userfile" ><br><br>
                <input type="submit" name="submit" value="UPLOAD" class="btn btn-primary">
            </form>

        <br><br>
        <table class="table table-striped table-hover table-bordered">
            <caption>Address Book List</caption>
            <thead>
                <tr>
                    <th>Id</th>
                    <th>SIP Id</th>
                    <th>SIP Password</th>
                    <th>Key</th>
                    <th>Name</th>
                    <th>Status</th>
                    <th>Email</th>
                    <th>Password</th>
                    <th>Balance</th>
                    <th>Created</th>
                    <th>Modified</th>
                    <th>Date_Inactive</th>
                    <th>Date Reset</th>

                </tr>
            </thead>
            <tbody>
                <?php if ($users == FALSE): ?>
                    <tr><td colspan="4">There are currently Users</td></tr>
                <?php else: ?>
                    <?php foreach ($users as $row): ?>
                        <tr>
                            <td><?php echo $row['id']; ?></td>
                            <td><?php echo $row['sip_id']; ?></td>
                            <td><?php echo $row['sip_pass']; ?></td>
                            <td><?php echo $row['key']; ?></td>
                            <td><?php echo $row['name']; ?></td>
                            <td><?php echo $row['status']; ?></td>
                            <td><?php echo $row['email']; ?></td>
                            <td><?php echo $row['password']; ?></td>
                             <td><?php echo $row['phone']; ?></td>
                            <td><?php echo $row['balance']; ?></td>
                            <td><?php echo $row['created']; ?></td>
                            <td><?php echo $row['modified']; ?></td>
                            <td><?php echo $row['date_inactive']; ?></td>
                            <td><?php echo $row['reset_date']; ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>

请检查我的代码并帮助我解决这个问题。当我上传 csv 时出现错误:

Message: Undefined index: id Message: Undefined index: sip_id Message: Undefined index: sip_pass

和所有其他字段。但是有些 id 和 date 是如何插入到表中的,而其他字段却没有!

我面临的错误:

最佳答案

如果您的模型返回 false,则用户 $users 数组为空。所以在 View 中你有你检查空

<?php if (!empty($users)): ?>// check empty array
    <tr><td colspan="4">There are currently Users</td></tr>
<?php else: ?>
    <?php foreach ($users as $row): ?>
        <tr>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['sip_id']; ?></td>
            <td><?php echo $row['sip_pass']; ?></td>
            <td><?php echo $row['key']; ?></td>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['status']; ?></td>
            <td><?php echo $row['email']; ?></td>
            <td><?php echo $row['password']; ?></td>
            <td><?php echo $row['phone']; ?></td>
            <td><?php echo $row['balance']; ?></td>
            <td><?php echo $row['created']; ?></td>
            <td><?php echo $row['modified']; ?></td>
            <td><?php echo $row['date_inactive']; ?></td>
            <td><?php echo $row['reset_date']; ?></td>
        </tr>
    <?php endforeach; ?>
<?php endif; ?>

关于php - Codeigniter 将部分 CSV 数据插入 MYSQL 只有 id 和 date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32644467/

有关php - Codeigniter 将部分 CSV 数据插入 MYSQL 只有 id 和 date的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  3. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  4. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  5. ruby CSV : How can I read a tab-delimited file? - 2

    CSV.open(name,"r").eachdo|row|putsrowend我得到以下错误:CSV::MalformedCSVErrorUnquotedfieldsdonotallow\ror\n文件名是一个.txt制表符分隔文件。我是专门做的。我有一个.csv文件,我转到excel,并将文件保存为.txt制表符分隔的文件。所以它是制表符分隔的。CSV.open不应该能够读取制表符分隔的文件吗? 最佳答案 尝试像这样指定字段分隔符:CSV.open("name","r",{:col_sep=>"\t"}).eachdo|row|

  6. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  7. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  8. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  9. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  10. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

随机推荐