This question already has an answer here:
- How to insert and move multiple image using codeigniter? 3 answers
view:
$("#submit").on('click',function(e){
e.preventDefault();
product_name = $("#product_name").val();
category = $("#category").val();
var formData = new FormData();
$.each($("#product_image"), function (i, obj) {
$.each(obj.files, function (j, file) {
formData.append('product_image[' + i + ']', file);
});
});
formData.append('product_name', product_name);
formData.append('category', category);
$.ajax({
type:"POST",
data:formData,
processData: false,
contentType: false,
url:"<?php echo base_url(); ?>admin/products",
success:function(data){
alert(data);
}
});
});
Controller:
public function products()
{
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['product_image']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['product_image']['name']= $files['product_image']['name'][$i];
$_FILES['product_image']['type']= $files['product_image']['type'][$i];
$_FILES['product_image']['tmp_name']= $files['product_image']['tmp_name'][$i];
$_FILES['product_image']['error']= $files['product_image']['error'][$i];
$_FILES['product_image']['size']= $files['product_image']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$dataInfo[] = $this->upload->data();
}
$data = array(
'product_name' => $this->input->post('product_name'),
'category' => $this->input->post('category'),
'product_image' => implode(",",array_column($dataInfo, 'product_image'))
);
$sql = $this->db->insert('add_product',$data);
if($sql == true)
{
echo 'New Product Added';
}
else
{
echo 'Unable to Proceed!';
}
}
private function set_upload_options()
{
$config = array();
$config['upload_path'] = ''.base_url().'resource/product/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
I have three input field i.e. product_name,category and product_image
. Now, I wants to move multiple images and insert product_image
name which is separated by comma(,) for example img1.jpg,img2.jpg,img3.jpg
like that. Now, when I alert reponse it show nothing. So, How can I solve ? Please help me.
expected output demo of mysql like
product_name category product_images
============ ======== ==============
men t-shirt MEN img1.jp,img2.jpg,img3.jpg
kids t-shirt kids img1.jp,img2.jpg,img3.jpg