猿问

从单个表单提交将数据插入到多个表中

我有下面提到的表格。


并获得以下输出。


帮助我使用 foreach 循环获得如下输出。


array('notification') = array('pos_id' => 'kiran','post_usr' => 'kumar','comment' => 'kaleti');

array('project') = array('Project_name' => 'india','Proejct_lang' => 'hyderabad');

这是控制器代码。


// controller

public function form_submit() { 

  $total_data = $this->input->post(); 

  print_r($total_data);

  //output :Array ( [notification|pos_id] => kiran [notification|post_usr] => kumar [notification|comment] => kaleti [project|Project_name] => india [project|Proejct_lang] => hyderabad )

  foreach ( $total_data as $key => $value ) { 

    $arr = explode("|",$key); 

    echo $arr[0]."--".$arr[1]."--".$value."<br />"; 

  }

}


富国沪深
浏览 329回答 1
1回答

侃侃无极

这是控制器代码,您将从中获取数据VIEW作为您将在controller.// Controller$data_array1 = array(&nbsp; &nbsp; 'table_field_name_here' => $this->input->post('pos_id'),&nbsp; &nbsp; 'table_field_name_here' => $this->input->post('post_usr'),&nbsp; &nbsp; 'table_field_name_here' => $this->input->post('comment'),);$data_array2 = array(&nbsp; &nbsp; 'table_field_name_here' => $this->input->post('Project_name'),&nbsp; &nbsp; 'table_field_name_here' => $this->input->post('Proejct_lang'),);&nbsp;&nbsp;$table_1 = 'table_name';$table_2 = 'table_name';$record_id_1 = $this->Common_model->insert_into_table($table_1, $data_array1);$record_id_2 = $this->Common_model->insert_into_table($table_2, $data_array2);if($record_id_1 && $record_id_2){&nbsp; &nbsp; // success ...!}else{&nbsp; &nbsp; // fail ...!&nbsp; &nbsp;&nbsp;}model您将从控制器调用的函数。// Modelfunction insert_into_table($table, $data) {&nbsp; &nbsp; // echo "<pre>asdf";print_r($data);exit;&nbsp; &nbsp; $insert = $this->db->insert($table, $data);&nbsp; &nbsp; $insert_id = $this->db->insert_id();&nbsp; &nbsp; if ($insert) {&nbsp; &nbsp; &nbsp; &nbsp; return $insert_id;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}希望您能了解使用模型将数据插入到两个表中是多么简单。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答