【问题标题】:How to insert the value simultaneously in two different table如何在两个不同的表中同时插入值
【发布时间】:2013-08-27 09:53:36
【问题描述】:

table1:subjectmaster

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

table2:course master

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

table3: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();
    }

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

如何获取值并将它们插入到表中?

【问题讨论】:

  • $data的结构是什么print_r就好了。

标签: php mysql codeigniter


【解决方案1】:

您的$data 数组应该将键作为course_table 的列

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

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

只需确保您使用正确的值和键创建了 $data 数组

【讨论】:

    【解决方案2】:

    试试这个:
    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;
        }
    ?>
    

    【讨论】:

    • 在模型中它告诉未定义的课程名称、主题名称、主题代码
    • 它基于你之前的模型函数,在 insert print_r($data);die; 之前查看 $data 的结构
    • 发布您的控制器,从您呼叫的位置function add_record($data)
    • 请帮我在两个不同的表中插入数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2018-05-03
    • 2014-01-18
    相关资源
    最近更新 更多