Codeigniter 将部分 CSV 数据插入到 MYSQL 中,只有 id 和 date

Posted

技术标签:

【中文标题】Codeigniter 将部分 CSV 数据插入到 MYSQL 中,只有 id 和 date【英文标题】:Codeigniter inserts partial CSV data into MYSQL only id and date 【发布时间】:2015-12-15 03:46:30 【问题描述】:

您好,我正在尝试通过上传 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] => )

控制器:

    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);

观点:

   <?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 时出现错误:

消息:未定义的索引:id 消息:未定义的索引:sip_id 消息:未定义的索引:sip_pass

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

我面临的错误:

【问题讨论】:

查看查看if(!empty($users)) foreach ($users as $row): 我要改成这个吗? 是的,因为如果 return FALSE; 来自您的模型,则意味着您的 $users 数组为空 对吗? 我从模型代码中得到了这个 else 部分:Nothing To Show 【参考方案1】:

如果您的模型 return false 用户 $users 数组为空。所以在视图中你有你检查空

<?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; ?>

【讨论】:

您好,这解决了空数组错误,但我的控制器或未定义 id、sip_id、sip_pass、key 的模型仍然出现错误....... 另外数据没有被导入mysql print_r( $csv_array) 并粘贴它的值 我得到了每条记录的数据库值数组 $csv_array = $this-&gt;csvimport-&gt;get_array($file_path);print_r( $csv_array)【参考方案2】:

消息:未定义索引:id 消息:未定义索引:sip_id 消息:未定义索引:sip_pass

当变量未定义或没有相关数据时会发生此错误

确保您的 $users 数组名称与此名称一致

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

并且在视图表中应该是

<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 (empty($users)) //use empty()
    
        ?>
        <tr><td colspan = "4" > There are currently Users </td ></tr>
    <?php
    
    else
    
        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
        
    
    ?>
    </tbody>
</table>

在模型中

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

    $count = count($result);

    if (empty($count))
    
        return $result;
    
    else
    

    

【讨论】:

以上是关于Codeigniter 将部分 CSV 数据插入到 MYSQL 中,只有 id 和 date的主要内容,如果未能解决你的问题,请参考以下文章

CodeIgniter - 将 CSV 上传到数据库问题

Codeigniter 将多个输入字段动态插入到数据库中?

如何使用 PHP CodeIgniter 将 CSV 数据导入 MYSQL 数据库?

一次运行多个mysql命令而不是在codeigniter中顺序运行?

将csv上传到Codeigniter

将查询输出到 csv、codeigniter