您好我正在尝试通过上传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和日期不要!
答案 0 :(得分:2)
如果您的模型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; ?>
答案 1 :(得分:1)
消息:未定义索引: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
{
}
}