2016-01-29 100 views
0

我想插入两个不同的值到我的数据库。如何将两个不同的值插入相同的字段?

字段名称是相同的,但我想要保存两个不同的值。

现在我有一个创建组织的窗体。因为我有两个领域的销售和技术。所以我插入姓名,姓氏,销售和技术的电子邮件以及。现在,无论我从哪里获取的值,我都会将其保存到用户表并将其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-> 

从POST数组代码

public function array_from_post($fields){ 
    $data = array(); 
    foreach ($fields as $field) { 
     $data[$field] = $this->input->post($field); 
    } 
    return $data; 
} 
+0

显示'$ this-> secure_m-> array_from_post'的代码,我认为问题在那里。 – ksimka

+0

@ksimka添加了代码 – Rajan

+0

我真的不明白你的问题是什么。 – Strawberry

回答

0
$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); 

,还可以使用这个数组很多表

+0

did not work我试过了返回错误 – Rajan

+0

您分配给一个数组并使用变量很多查询。 –

+0

是的,我试图插入销售的方式,但显示空白的白色屏幕,我想它是一个错误 – Rajan

0

你试图从错误的键获取值。即你有first_name_salesfirst_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查看。 Smth like

$this->load->view('admin/_layout_main', ['user_tech' => $data_tech, 'user_sales' => $data_sales]); 
+0

请查看更新的代码,我想插入两个值在表单用户与一个单一的形式 – Rajan

+0

而这正是我的建议。现在你有6个字段的表格,但其中3个与3个字段具有相同的名称。您必须更改这些名称才能使其工作。这是HTML表单的基础知识。或者,如果您认为我错了,请显示您的表单的最终HTML。 – ksimka

相关问题