doru52911 2018-12-02 23:31
浏览 50
已采纳

使用Codeigniter将最后插入的记录ID插入另一个表中的数组中

so I got two tables one called patients and another called tests , the tests table has the patient id , i have a page called add patient which have fields for adding new patient information and other fields for adding their test info and upload all the data into the two tables in one query ,tests fields could be duplicated by ajax to i could add more than one test to the same patient , now i wanna add more than one test at the same time into the tests table and i managed to do so but the case is i can't add the patient_id into tests table, i wanna add the same patient_id more than one time to all the tests i have added while adding a new patient in that page, i'm new to Codeigniter! this is the adding page

the patient fields and the test fields

<input type="text" name="patientname" />
<input type="text" name="address" />

<input type="text" name="testname[]"/>
<input type="text" name="price[]" />

and this is my controller

public function testbby
{
    $this->load->model("khmodel", "Khmodel");

    // patient main info
    $patient_input_data = array();
    $patient_input_data['patientname'] = $this->input->post('patientname');
    $patient_input_data['address'] = $this->input->post('address');

    //test data
    $testname = $this->input->post('testname[]');
    $price = $this->input->post('price[]');

    $test_input_data = array();
    for ($i = 0; $i < count($testname); $i ++ )
    {
        $test_input_data[$i] = array(
            'testname' => $testname[$i],
            'price' => $price[$i],
        );
    }
    $this->Khmodel->insert_bby($patient_input_data, $test_input_data);

    redirect('main/dashboard');
}

and this is my model

public function insert_bby($patient, $test)
{
    $this->db->insert('patients', $patient);
    $patient_id = $this->db->insert_id();

    // i used this and it worked but only while adding one test , s
    //once it's gonna be an array i dunno what to do with the id !
    //$test['refpatient']=$patient_id;
    $this->db->insert_batch('tests', $test);

    return $insert_id = $this->db->insert_id();
}
  • 写回答

2条回答 默认 最新

  • dongti8535 2018-12-06 17:25
    关注

    the value i mean , i dont know but your code seems to be so right and logical but i have tried this code and it worked so well , i didn't even use the model/

     public function testbby
    {
    $this->load->model("khmodel", "Khmodel");
    
    // patient main info
    $patient_input_data = array();
    $patient_input_data['patientname'] = $this->input->post('patientname');
    $patient_input_data['address'] = $this->input->post('address');
    
    //test data
    $testname = $this->input->post('testname[]');
    $price = $this->input->post('price[]');
    
        $this->db->reset_query();
        $this->db->insert('patients', $patient_input_data);
        $patient_id=$this->db->insert_id();
    $test_input_data = array();
    for ($i = 0; $i < count($testname); $i ++ )
    {
        $test_input_data[] = array(
            'testname' => $testname[$i],
            'price' => $price[$i],
            'patient_id'=>$patient_id
    
        );
    }
      $this->db->reset_query();
      $this->db->insert_batch('tbl_tests',$test_input_data);
       redirect('main/dashboard');
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。