如何单击一次将数据插入两个MySql表?
型号代码
public function insertCSV($data)
{
$this->db->insert('question_tb', $data);
$this->db->insert('option_tb', $data);
return TRUE;
}
控制器代码
public function import(){
if (isset($_POST["import"])) {
$this->load->model('Welcome_model', 'welcome');
$exam_id = $this->welcome->get_max_exam_id();
$filename = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($filename, "r");
while (($importdata = fgetcsv($file, 10000, ",")) !== false) {
$data = [
'question' => $importdata[0],
'answer' => $importdata[1],
'exam_id' => $exam_id,
];
while (($importdata = fgetcsv($file, 10000, ",")) !== false) {
$data = [
'option1' => $importdata[2],
'option2' => $importdata[3],
'option3' => $importdata[4],
'exam_id' => $exam_id,
];
$insert = $this->welcome->insertCSV($data);
}
//fclose($file);
fclose($file);
redirect('upload/index');
}
}
}
}
答案 0 :(得分:0)
有两个选择
型号:
function a($data1){
$this->db->insert('table1', $data1);
}
function b($data2){
$this->db->insert('table2', $data2);
}
控制器:
function insert(){
$this->model_name->a($data1);
$this->model_name->b($data2);
}
型号:
function a($data1, $data2){
$this->db->insert('table1', $data1);
$this->db->insert('table2', $data2);
}
控制器:
function insert(){
$this->model_name->a($data1, $data2);
}