gpt4 book ai didi

php - 如何使用codeigniter在mysql中的单列中插入数组数据

转载 作者:行者123 更新时间:2023-11-29 01:20:17 24 4
gpt4 key购买 nike

我正在尝试使用 codeigniter 将数组数据插入数据库。当我使用 print_r() 打印该值时很有趣。

它显示正确的结果,但是当我点击保存按钮时,它会在表格中插入空白值。

我只想在处方表中为特定的 prescription_id 存储多个药物名称我正在尝试使用 insert_batch 函数插入数据,但它保存了空白记录

这是我的模型代码:

public function add_prescription($data) {

$data['medicine_name'] = $data['medicine_nm[]'];
print_r($data['medicine_name']);

$this->db->insert_batch('pre',$data);
return $this->db->insert_id($pre_id);
}

Controller 代码--

public function prescription($patient_id = NULL, $app_date = NULL, $hour = NULL , $min = NULL) {

//Check if user has logged in
if (!$this->session->userdata('user_name') || $this->session->userdata('user_name') == '') {
redirect('login/index/');

} else {

if ($this->form_validation->run() === FALSE) {
$data['medicine_nm[]']=$this->input->post('medicine_nm[]');
$this->patient_model->add_prescription($data);
}
}
}

最佳答案

您需要对该数组进行json_encode,然后像这样将其插入数据库

$data = json_encode($array);

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

在抓取的同时你需要对其进行decode

$data = json_decode($column_result);

希望对你有帮助。

关于php - 如何使用codeigniter在mysql中的单列中插入数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39544809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com