嗨,我正在尝试通过上传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时出现错误:
Message: Undefined index: id
Message: Undefined index: sip_id
Message: Undefined index: sip_pass
和所有其他领域.但是,某些ID和日期如何插入表格和其他字段中却没有!
解决方法:
Message: Undefined index: id Message: Undefined index: sip_id Message: Undefined index: 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
{
}
}