gpt4 book ai didi

php - 在codeigniter中插入数据库的图像路径

转载 作者:行者123 更新时间:2023-11-29 20:48:04 25 4
gpt4 key购买 nike

我有一个包含文本字段和单个图像的表单,文本字段已成功插入数据库,但图像路径未插入我存储在图像变量中的数据库中,我正在使用 mysql 数据库,图像正在上传在文件夹中,但路径未存储在数据库中。

Controller 文件

class product{
function validate_products()
{


$this->form_validation->set_rules('title', 'title', 'trim|required|');
$this->form_validation->set_rules('price', 'price', 'trim|required');
$this->form_validation->set_rules('description', 'description', 'trim|required');
$this->form_validation->set_rules('category', 'category', 'trim|required');
//$this->form_validation->set_rules('image', 'image', 'trim|required');

$config = array(
'upload_path' => "./images/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => false,
);



$this->load->library('upload', $config);



if($this->form_validation->run() && $this->upload->do_upload() )
{

$data = $this->upload->data();
$image= base_url("images/". $data['raw_name'] . $data['file_ext']);

$post['image'] = $image;



$this->load->model('upload_model');

if($query= $this->upload_model->create_product())
{
$this->session->set_flashdata('product_sucsess', 'Product uploaded sucsessfully');
$this->upload();

}
else
{


$this->session->set_flashdata('product_fail', 'Sorry product not uploaded .');
$this->load->view('Admin/upload_product');

}


}
else
{

$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/upload_product', $error);


}
}
}

模型类

class upload_model extends  CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}



function create_product()
{
$new_member_insert_data = array(
'title'=> $this->input->post('title'),
'image'=> $this->input->post('image'),
'price'=> $this->input->post('price'),
'description'=> $this->input->post('description'),
'category'=> $this->input->post('category'));

$insert = $this->db->insert('product', $new_member_insert_data);
return true;
}

}

最佳答案

$post['image'] = $image; 不经过 $this->input->post('image')。

您可以尝试将 $post['image'] 声明为 $_POST['image']。

如果这不起作用,那么您将需要手动将变量传递给 create_product 函数。

关于php - 在codeigniter中插入数据库的图像路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38290662/

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