[英]how to insert array data in single column in mysql using codeigniter
I'm trying to insert array data into database using codeigniter. 我正在尝试使用codeigniter将数组数据插入数据库。 when I print that values using
print_r()
fun. 当我使用
print_r()
来打印该值时很有趣。
It shows correct result, but when I click on save button it inserts blank values in table. 它显示正确的结果,但是当我单击“保存”按钮时,它将在表中插入空白值。
I just want to store multiple medicine names in prescription table for particular prescription_id i am trying to insert data using insert_batch function but it saves blank records 我只想在特定处方表中的处方表中存储多个药物名称,我正在尝试使用insert_batch函数插入数据,但是会保存空白记录
this is my model code: 这是我的模型代码:
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 code-- 控制器代码-
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);
}
}
}
You need to json_encode
that array and then insert it into the database like 您需要
json_encode
该数组,然后将其插入数据库,例如
$data = json_encode($array);
$this->db->insert('column_name',$data);
And while fetching you need to decode
it 在获取时,您需要对其进行
decode
$data = json_decode($column_result);
Hope this helps you. 希望这对您有所帮助。
try this 尝试这个
public function add_prescription($name) {
$data = array(
'medicine_name' => $name
);
$query = $this->db->insert('table_name',$data);
return $query->result_array();
}
Hope this will help you 希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.