如何在两个不同的表中同时插入值

时间:2013-08-27 09:53:37

标签: php mysql codeigniter

table1:subjectmaster

id | subjectcode | subjectname
------------------------------
1  | phy         | physics

表2:course master

id | coursecode | coursename
----------------------------
1  | bsc        | bachlore of science

表3:examcourse

id | examname | course_code
---------------------------
1 | semester  | bsc

table4:course_table

id | coursecode | coursename |  subjectcode | subjectname | examname
--------------------------------------------------------------------

型号:

function add_record($data)
{
    $this->db->insert('examcourse', $data);
    $this->db->insert('course_table', $data);
    return;
}

控制器:

function create()
    {
    $j=1;
     $createcustomer = $this->input->post('createcustomer');

         if( $this->input->post('createcustomer') != false ){

       foreach ($createcustomer as $j)
       {
            $data = array(
            'exam_name' => $this->input->post('exam_name_id'.$j),
            'course_code' => $this->input->post('course_code_id'.$j)
            );

            //$course_code= mysql_real_escape_string($_POST["course_code_id".$j]);
            $exam_name = $this->input->post('exam_name_id'.$j);
            if ($exam_name != ""){
            $this->examcourse_model->add_record($data, $exam_name);
            }
            $j++;

    }
    }
        $this->index();
    }

如果我点击插入表单,它只会在 table3 table4 中插入coursecodeexamname 但我需要在 table4 中插入coursecodecoursenamesubjectcodesubjectnameexamname

如何获取值并将其插入表中?

2 个答案:

答案 0 :(得分:0)

您的$data数组的键应为course_table

$data=array(
'coursecode'=>'test',
'coursename'=>'test',
'subjectcode'=>'test,'
'subjectname'=>'test',
'examname'=>'test'
);

$this->db->insert('course_table', $data);

确保使用正确的值和键

创建$data数组

答案 1 :(得分:0)

试试这个:
像这样从data array中检索值:

<?php
    function add_record($data)
    {
        $temp   = array('coursecode'=>$data['coursecode'], 
                    'coursename'=>$data['coursename'],
                    'subjectcode'=>$data['subjectcode'],
                    'subjectname'=>$data['subjectname'],
                    'examname'=>$data['examname']
                    );
        $this->db->insert('course_table', $temp);
        $this->db->insert('examcourse', $data);
        $this->db->insert('course_table', $data);
        return;
    }
?>