我是新代码点火器。 这是我想要做的。我有存储在数据库表名产品中的产品列表。对于我需要插入多个图像的每个产品。我创建了两个表格,产品和productimage。我已经将表productimage的product_id作为外键,引用表产品的product_id。现在我想从表单中保存数据。这是我以前做过的Saving images in a single row by imploding 但是对我来说,管理CRUD变得相当困难(就像编辑和删除图片一样)。 所以我想要做上述提到的方式。我没有找到开始的方法。任何人都可以请教我,我该如何开始?在codeigniter中使用一对多保存多个图像
好吧,现在我已经在这里做了一些编码。这是我的控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Products extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('product_model');
$this->load->helper(array('form','url'));
//Codeigniter : Write Less Do More
}
public function index()
{
$data['products']=$this->product_model->get_product();
$this->load->view('/landing_page',$data);
}
public function create()
{
#code
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('product_name','Product_Name','required');
if($this->form_validation->run()=== FALSE)
{
$this->load->view('products/create');
}
else {
$this->product_model->set_product();
$data['products']=$this->product_model->get_product();
redirect('/');
}
}
}
这是我的模型:当我的表单打开我被能填补产品名称,上传图片文件
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_Model{
public function __construct()
{
$this->load->database();
parent::__construct();
//Codeigniter : Write Less Do More
}
public function get_product()
{
#code
$query=$this->db->get('products');
return $query->result_array();
}
public function set_product($id=0)
{
#code
// if($this->input->post('userSubmit')){
$picture=array();
$count=count($_FILES['picture']['name']);
//Check whether user upload picture
if(!empty($_FILES['picture']['name'])){
foreach($_FILES as $value)
{
for($s=0; $s<=$count-1; $s++)
{
$_FILES['picture']['name']=$value['name'][$s];
$_FILES['picture']['type'] = $value['type'][$s];
$_FILES['picture']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['picture']['error'] = $value['error'][$s];
$_FILES['picture']['size'] = $value['size'][$s];
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture[] = $uploadData['file_name'];
}
else{
$picture = '';
}
}
}
}//end of first if
else{
$picture = '';
}
$data=array(
'product_name'=>$this->input->post('product_name')
);
$picture=array(
'product_id'=>$this->db->get('products',$id),
'picture_image'=>$picture
);
if ($id==0)
{
return $this->db->insert('products',$data);
return $this->db->insert('images',$picture);
}
else {
$this->db->where('id',$id);
return $this->db->update('products',$data);
return $this->db->update('images',$picture);
}
}
}
诺埃的情况。当我提交它时也不会引发任何错误。但是只有产品名称存储在产品表中,并且图像表没有任何反应。没有插入任何图像。浏览器不会引发任何错误。简单图像 表是空的。这里有什么问题?
首先,您可以用最少的必填字段创建产品,然后将图像上传到该产品。在magento中,他们是这样做的。 – kishor10d
哦,你不上传模型中的图像。虽然CI不会阻止你。建议您在控制器上传图片 –