![](/img/trans.png)
[英]php retrieving id of the last record inserted and insert it in another table
[英]insert the last inserted record id into an array in another table using Codeigniter
因此,我得到了两个表,一个称为“患者”,另一个称为“测试”,测试表具有“患者ID”,我有一个名为“添加患者”的页面,该页面具有用于添加新患者信息的字段,以及用于添加其测试信息并将所有数据上传到其他字段的字段。一个查询中的两个表,可以通过ajax复制测试字段,以便我可以向同一个患者添加多个测试,现在我想同时向测试表中添加多个测试,但是我设法这样做情况是我无法将耐心_id添加到测试表中,我想在同一页面中添加新患者的同时,向我添加的所有测试中多次添加相同的耐心_id,我是Codeigniter的新手! 这是添加页面
患者领域和测试领域
<input type="text" name="patientname" />
<input type="text" name="address" />
<input type="text" name="testname[]"/>
<input type="text" name="price[]" />
这是我的控制器
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');
}
这是我的模特
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();
}
首先,您不需要这个。
$patient_input_data = array();
因为当你打电话时
$patient_input_data['patientname'] = $this->input->post('patientname');
将创建数组$patient_input_data
。 有时您可能想要确保您有一个即使是空的数组。 但这不是其中之一。
对于数组输入值,即testname[]
,通过在名称末尾省略方括号来获取数据。 像这样。
//test data
$testname = $this->input->post('testname'); //instead of post('testname[]')
$price = $this->input->post('price');
变量$testname
和$price
将是数组,其中包含表单上每个字段的项目。
在我看来,这两个输入是必需的,因此您应该添加代码以检查情况是否如此。 为此, 表单验证类非常出色。
数组$test_input_data
是你将要在阵列存在的情况下-即使它是空的。 您不必添加项目时,该阵列,即明确设置的索引值$test_input_data[$i] = array(...
因为$test_input_data[] = array(...
会工作得很好,但没有伤害任何一种方式。
进入模型。 第一部分是好的。 对于第二个,您需要创建一个数组,其中包含您从第一个插入中获得的患者ID,并将该值添加到$tests
参数中的每个子数组中。 然后模型变成这个。
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);
}
我的意思是我不知道的值,但是您的代码似乎是正确和逻辑的,但是我尝试了此代码,并且效果很好,甚至没有使用模型/
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.