我想在我的数据库中插入两个不同的值。
字段名称相同,但我希望在那里保存两个不同的值。
现在,我有一个创建组织的表单。 因为我有两个领域销售和技术。所以我也插入了名称和技术,last_name,电子邮件。现在,无论我从那里获得什么值,我都会将其保存到用户表,并将其ID保存到我的组织表中。
首先我要插入销售人员信息,然后是技术人员 信息并获取他们的 ID 并将其保存在组织中
这是我的代码:
$this->data['company'] = $this->company_m->get_new();
$this->data['user'] = $this->secure_m->get_new();
$rules = $this->company_m->rules_admin;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE)
{
$data =$this->secure_m->array_from_post(array('first_name','last_name','email'));
$sales_id = $this->secure_m->save($data,$id);
$data =$this->secure_m->array_from_post(array('first_name','last_name','email'));
$tech_id = $this->secure_m->save($data,$id);
$data = $this->company_m->array_from_post(array('org_name','dba','addr1','addr2','city','state','country','pin','sales_id','tech_id','tax_number','comment','url'));
$data['sales_id']= $sales_id;
$data['tech_id']= $tech_id;
$this->company_m->save($data, $id);
redirect('admin/company');
}
// Load the view
$this->data['subview'] = 'admin/company/add';
$this->load->view('admin/_layout_main', $this->
开机自检代码中的阵列
public function array_from_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
您正在尝试从错误的键中获取值。 即你有first_name_sales
和first_name_tech
,但总是从first_name
中读取。
尝试
$data_tech = $this->secure_m->array_from_post(array('first_name_tech','last_name_tech','email_tech','tech'));
// ...
$data_sales = $this->secure_m->array_from_post(array('first_name_sales','last_name_sales','email_sales','sales'));
// ...
此外,您应该从相应的对象而不是相同的$user
set_value
。像$user_tech
一样通过,$user_sales
查看。喜欢
$this->load->view('admin/_layout_main', ['user_tech' => $data_tech, 'user_sales' => $data_sales]);
$values = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'user_type' => $this->input->post('user_type'),
);
$data = $this->model->inset('table_name', $values);
并且还将此数组用于许多表