简体   繁体   English

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

[英]insert the last inserted record id into an array in another table using Codeigniter

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! 因此,我得到了两个表,一个称为“患者”,另一个称为“测试”,测试表具有“患者ID”,我有一个名为“添加患者”的页面,该页面具有用于添加新患者信息的字段,以及用于添加其测试信息并将所有数据上传到其他字段的字段。一个查询中的两个表,可以通过ajax复制测试字段,以便我可以向同一个患者添加多个测试,现在我想同时向测试表中添加多个测试,但是我设法这样做情况是我无法将耐心_id添加到测试表中,我想在同一页面中添加新患者的同时,向我添加的所有测试中多次添加相同的耐心_id,我是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();
}

To start with, you don't need this. 首先,您不需要这个。

$patient_input_data = array();

Because when you make the call 因为当你打电话时

$patient_input_data['patientname'] = $this->input->post('patientname');

The array, $patient_input_data , will be created. 将创建数组$patient_input_data There are times when you might want to make sure you have an array even if it's empty. 有时您可能想要确保您有一个即使是空的数组。 But this isn't one of them. 但这不是其中之一。

For the array input values, ie testname[] , get the data by leaving off the brackets at the end of the name. 对于数组输入值,即testname[] ,通过在名称末尾省略方括号来获取数据。 Like this. 像这样。

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

The vars $testname and $price will be arrays with an item for each field on the form. 变量$testname$price将是数组,其中包含表单上每个字段的项目。

Seems to me that those two inputs are required so you should add code to check that is the case. 在我看来,这两个输入是必需的,因此您应该添加代码以检查情况是否如此。 The Form Validation class is excellent for that purpose. 为此, 表单验证类非常出色。

The array $test_input_data is a case where you will want the array to exist - even if it's empty. 数组$test_input_data是你要在阵列存在的情况下-即使它是空的。 You don't have to explicitly set the index value when adding items to the array, ie $test_input_data[$i] = array(... because $test_input_data[] = array(... will work just fine, but there's no harm either way. 不必添加项目时,该阵列,即明确设置的索引值$test_input_data[$i] = array(...因为$test_input_data[] = array(...会工作得很好,但没有伤害任何一种方式。

On to the model. 进入模型。 The first part is good. 第一部分是好的。 For the second you need to create arrays that include the patient id you got from the first insert and add that value to each of the sub-arrays in the $tests argument. 对于第二个,您需要创建一个数组,其中包含您从第一个插入中获得的患者ID,并将该值添加到$tests参数中的每个子数组中。 The model then becomes this. 然后模型变成这个。

public function insert_bby($patient, $tests)
{
    $this->db->insert('patients', $patient);
    $patient_id = $this->db->insert_id();
    // add the patient id key/value to each sub-array in $tests
    foreach ($tests as $test)
    {
        $test['patient id'] = $patient_id;
    }
    // will return the number of rows inserted or FALSE on failure
    return $this->db->insert_batch('tests', $tests);
}

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');
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP检索插入的最后一条记录的ID,并将其插入另一个表 - php retrieving id of the last record inserted and insert it in another table 如何在另一个表中插入最后插入的ID - How to Insert last inserted id in another table 在CodeIgniter中使用批量插入获取查询的最后一个插入ID时出错 - Error in getting the last inserted ID of a query using batch insert in CodeIgniter 使用Codeigniter在文本框中显示最后插入的记录 - Show last inserted record in Textbox using Codeigniter 检索表的最后插入ID以在另一个表中用作外键 - retrieving last inserted ID of a table for using as Foreign key in another table 如何获取一个表上的最后一个插入ID,以在另一个表上的另一个插入中使用它? - How to get the last inserted id on a table to use it on another insert on another table? 在不使用mysql_insert_id()的情况下获取最后插入的记录的ID - get id of last inserted record without using mysql_insert_id() 插入单个记录,获取最后插入的ID,然后进行多次插入 - Insert single record, get last inserted ID and then do multiple insert 想要将一个表的最后插入的ID插入laravel 5中的另一个表 - Want to insert one table's last inserted id into another table in laravel 5 CodeIgniter:如何将$ this-&gt; db-&gt; insert_id()值传递给Controller以便插入到另一个表中? - CodeIgniter: How to pass $this->db->insert_id() value to Controller in order to be inserted in another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM