【发布时间】:2017-03-08 11:26:35
【问题描述】:
我有一个看起来像这样的数组
Array ( [0] => test1 [1] => test4 [2] => test2 )
我使用 Codeigniter 内置函数从我的数据库中获取了这个值
每当我尝试将此值插入我的数据库中时,它都会插入 index 而不是 value 本身
如您所见,它不是在用户名下的字段中存储test1、test4、test2,而是存储索引0、1、2。
请问如何解决?
参考资料: @MichaelK
表格:
查看
<div class="panel-body">
<?php echo form_open('admin/add_recommended'); ?>
<div class="form-group col-lg-12">
<label>Recommended Employees:</label>
<?php echo form_error('skillsRequired'); ?>
<?php
foreach ($users as $row) {
$user[] = $row->username;
}
print_r($user);
echo form_multiselect('user[]', $user, $user, array('class' => 'chosen-select', 'multiple style' => 'width:100%;'));
?>
</div>
</div>
<div class="panel-footer">
<?php echo form_submit(array('id' => 'success-btn', 'value' => 'Submit', 'class' => 'btn')); ?>
<?php echo form_close(); ?>
</div>
控制器
public function add_recommended() {
$this->form_validation->set_rules('skillsRequired', 'Skills Required', 'min_length[1]|max_length[55]');
$lid = $this->admin_model->getID();
foreach ($lid as $id) {
$last_id = $id['projectID'];
$data['users'] = $this->admin_model->getUsers($last_id);
}
$this->load->view('admin/projects/rec-employee', $data);
if ($this->form_validation->run() === FALSE) {
//$this->load->view('admin/projects/rec-employee');
} else {
$users = $this->input->post('user');
print_r($users);
foreach ($users as $user) {
$data = array(
'projectID' => $last_id,
'username' => $user
);
$id = $this->admin_model->insert('projectemp', $data);
}
if ($id) {
$this->session->set_flashdata('msg', '<div class="alert alert-success" role="alert">Success! New Project has been added.</div>');
redirect('admin/add_recommended');
}
}
}
【问题讨论】:
-
您正在使用来自 POST 请求的输入,例如
$this->input->post('user')。这个输入应该是一个数组吗?如果尝试print_r($users)或var_dump($users)会得到什么结果? -
你能在你的问题中显示 print_r $users 数组吗?
-
@MichaelK 是的,因为我希望将多个值保存在我的数据库中。给定该示例:projectID = 106,用户名 = test1 ||项目 ID = 106,用户名 = test4。类似的东西
-
@rahul_m print_r 是数组([0] => test1 [1] => test4 [2] => test2)
-
@MichaelK 数组 ([0] => test1 [1] => test4 [2] => test2)
标签: php arrays codeigniter