我想将两个不同的值插入到我的数据库中。
字段名称相同,但我希望在那里保存两个不同的值。
现在我有一个创建组织的表单。我有两个领域:销售和技术。因此,我还插入了销售和技术人员的姓名、姓氏、电子邮件。现在,无论我从那里获得什么值,我都会将其保存到用户表中,并将他们的 ID 保存到我的组织表中。
First i want insert sales person information and then tech person information and get their id and save it in organization
这是我的代码:
$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->
来自 POST 代码的数组
public function array_from_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
最佳答案
您正尝试从错误的键获取值。 IE。您有 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'));
// ...
此外,您应该从相应的对象中set_value
而不是相同的$user
。通过 $user_tech
和 $user_sales
来查看。有点像
$this->load->view('admin/_layout_main', ['user_tech' => $data_tech, 'user_sales' => $data_sales]);
关于php - 如何在同一个字段中插入两个不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35078847/