Codeigniter只在MYSQL id和日期中插入部分CSV数据

时间:2021-05-08 16:25:53

Hi i am trying to insert data into mysql table by uploading the CSV. But i am getting an error. the upload shows me error and inserts only the id and date. I dont know the possible reasons for it. Firstly i exported the users table from MYSQL to csv format. Now i upload same using my code but does not work Below is my code :

你好,我正在尝试通过上传CSV将数据插入mysql表。但是我得到了一个错误。上载显示错误并只插入id和日期。我不知道可能的原因。首先我将users表从MYSQL导出为csv格式。现在我用我的代码上传同样的东西,但下面不工作的是我的代码:

I did Print_r($file_data); so got this

我做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] => )

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


    }

The Model :

模型:

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

}

}

The 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>

Please Review my code and help me solve this issue. When i upload the csv i get an error :

请查看我的代码,帮我解决这个问题。当我上传csv时,我得到一个错误:

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

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

and all other fields. But some how id and date get inserted in table and other fields dont!

和所有其他字段。但是,一些id和日期是如何插入到表和其他字段的呢?

The Error I face : Codeigniter只在MYSQL id和日期中插入部分CSV数据

我面临的错误是:

2 个解决方案

#1


1  

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

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

this error occurs when variable is not define or no data related with it

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

Make sure your $users array names are with this names

请确保您的$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'];

and in view table should be

视图表应该是

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

and in model

在模型

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

    $count = count($result);

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

    }
}

#2


2  

If your model return false the user $users array is empty. So in view you have you check empty

如果模型返回false,则用户$users数组为空。所以你要勾选为empty

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

#1


1  

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

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

this error occurs when variable is not define or no data related with it

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

Make sure your $users array names are with this names

请确保您的$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'];

and in view table should be

视图表应该是

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

and in model

在模型

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

    $count = count($result);

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

    }
}

#2


2  

If your model return false the user $users array is empty. So in view you have you check empty

如果模型返回false,则用户$users数组为空。所以你要勾选为empty

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