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

标签 php mysql codeigniter

表1:主题大师

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

表2:类(class)大师

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

表3:考试类(class)

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

表4:类(class)表

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

型号:

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

Controller :

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
但我需要插入coursecodecoursenamesubjectcodesubjectnameexamname表4中。

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

最佳答案

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

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

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

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

关于php - 如何在两个不同的表中同时插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462263/

相关文章:

php - Paypal 捐赠你喜欢的下载

php - 日期之间的错误搜索

php - id 不是 auto_increment 并试图在行之间获取差异 mysql php

sql - "SELECT TOP 1"codeigniter 是否相等?

php - Symfony 3.2.1 从 yml 文件值中删除下划线

Javascript多图像颜色变化

php - mysql 存储日期和时间的格式是什么?

mysql - rails : How update a model without Table

php - 如何自定义smarty中的错误处理?

php - codeigniter 3.0-dev 随机删除 session