gpt4 book ai didi

mysql - 图像未使用codeigniter插入mysql

转载 作者:行者123 更新时间:2023-11-29 02:40:22 36 4
gpt4 key购买 nike

我正在尝试将图像与其他数据一起插入到 mysql 数据库中,但它显示错误。它显示未保存的 $msg 和重复 View 文件的数据可能是由于我设置的 $error。PS:我在数据库中设置了'image'数据类型varchar。这是我的 View 文件:

        <input type="file" class="form-control-file" name="image" id="exampleInputFile" >

这是我的 Controller :

 public function save()
{
$this->load->model('Partner_model');
$feature = $this->input->post('feature');

$config['upload_path'] = './uploads/files';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('partner_profile', $error);
}
else
{


$user_data= array(

'pname' => $this->input->post('pname'),
'type' => $this->input->post('type'),
'address' => $this->input->post('address'),
'about' => $this->input->post('about'),
'city' => $this->input->post('city'),
'code' => $this->input->post('code'),
'state'=>$this->input->post('state'),
// 'image'=>$this->upload->do_upload('image')
'feature'=>implode(",",$feature),
'image' => $this->upload->data()

);
}

if($this->Partner_model->save($user_data))
{
$msg = "save sucesss" ;
}
else
{
$msg = "not save";
}

$this->session->set_flashdata('msg', $msg);
$this->load->view('partner_profile');
}
}

这是我的模型:

    public function save($data)
{

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

}

最佳答案

您的表单必须在 HTML 文件中具有 multipart 属性,如下所示:

如果你使用的是表单助手,那么它应该是

<?php echo form_open_multipart('/save');?>

否则你的表单应该有如下的 enctype 属性

<form enctype="multipart/form-data">

那么上传的数据结果$this->upload->data()就会排成数组。所以你不能将数组存储在 mysql 列中。因此,您需要从 $this->upload->data() 获取文件名并将其存储在如下变量中。

你的 Controller 应该是

public function save(){
$this->load->model('Partner_model');
$feature = $this->input->post('feature');

$config['upload_path'] = './uploads/files';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('partner_profile', $error);
}else{
$imageArray = $this->upload->data();
$image = $imageArray['file_name'];
$user_data= array(
'pname' => $this->input->post('pname'),
'type' => $this->input->post('type'),
'address' => $this->input->post('address'),
'about' => $this->input->post('about'),
'city' => $this->input->post('city'),
'code' => $this->input->post('code'),
'state'=>$this->input->post('state'),
'feature'=>implode(",",$feature),
'image' => $image
);
}
if($this->Partner_model->save($user_data)) {
$msg = "save sucesss" ;
}else {
$msg = "not save";
}
$this->session->set_flashdata('msg', $msg);
$this->load->view('partner_profile');
}

关于mysql - 图像未使用codeigniter插入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53906516/

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