求助阅读下面的评论
所以我想在codeigniter中使用modal上传图片,答案就在我的眼前,它正好在这个链接上https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html,但问题是,在那个网站上它只显示一个动作表单上传不使用任何模态并且不将其插入任何数据库,我已经尝试了但是因为我不擅长编码而更不用说刚刚学会了如何使用codeigniter就像昨天一样,所以我几乎不明白如何使用模态实现它笨
事情是我已经做了几个方法来实现它,但它似乎我的模态得到一个错误
所以这是代码
CONTROLLER
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Barang extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('Crud');
}
public function index(){
$data['tab3'] = true;
$data['query'] = $this->Crud->read('barang', null, 'idbarang', 'DESC');
$this->load->view('admin/barang_halaman', $data);
}
//insert data into database
public function insert(){
$nama = $this->input->post('namaBarang');
$harga = $this->input->post('harga');
$stock = $this->input->post('stock');
$idkategori = $this->input->post('kategori');
//====================================================================
//$img is a variable to put the image that has been inputed
//====================================================================
$img= $this->input->post('foto');
$data = array('nama'=>$nama, 'harga'=>$harga, 'stock'=>$stock, 'idkategori'=>$idkategori,'foto'=>$img);
$insert = $this->Crud->create('barang', $data);
redirect(($_SERVER['HTTP_REFERER']), 'refresh');
}
//========================================================================
//upload image
//source of this code i got it from the link that i already provide above, but i don't know how to use it
//========================================================================
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('filename'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
这是我的观点
查看
<!-- Insert data using modal -->
<div id="addModal" class="modal fade" role="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title"><span class="glyphicon glyphicon-plus"></span> Tambah Barang</h3>
</div>
<div class="modal-body">
<form action="<?php echo site_url('admin/barang/insert');?>" method="post" enctype="multipart/form-data">
//==================================================
//Heres the code that i use in codeigniter to upload images,
//but it's seems i'm getting error using this code
//==================================================
<div class="form-group">
<label>Foto</label>
<input type="file" name="foto" id="foto" class="form-control" action="<?php echo site_url('admin/barang/do_upload');?>">
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
</form>
</div>
</div>
</div>
</div>
<!-- Insert data using modal end-->
下面是我得到的错误,我认为它是因为查询或某种程度我不知道如何使用它的功能。
错误号码:1048
列'foto'不能为空
INSERT INTO
barang
(nama
,harga
,stock
,idkategori
,foto
)VALUES('Meja Baru','10000 ','5','2',NULL)文件名:C:/xampp/htdocs/PWI/tubes/AKS-master/application/models/Crud.php
行号:11
答案 0 :(得分:1)
您选择的文件名不会出现在后置阵列中。它将出现在composer.json
数组
要获取所选图像的名称,
$_FILES
$img= $_FILES['foto']['name'];// this will have original name
您要调用insert()
上传文件的位置。
答案 1 :(得分:1)
这是我处理上传过程的方式。但我认为您的问题在于您的MYSQL表设置。检查foto字段是否设置了允许Null设置。您可能无法将foto值设置为null,并且数据库正在抛出您获得的错误。
控制器功能:
public function save_file() {
$config['upload_path'] = './accounts/uploads';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 500;
$config['max_width'] = 1600;
$config['max_height'] = 1600;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file')){
//upload was not successful
}else {
//upload was successful
$this->load->model('products_model');
$photo_data['account_id'] = $this->account_id;
$photo_data['product_id'] = $this->input->post('record_id');
$photo_data['file_name'] = $this->upload->data('file_name');
$photo_data['file_type'] = $this->upload->data('file_type');
$photo_data['file_path'] = $this->upload->data('file_path');
$photo_data['full_path'] = $this->upload->data('full_path');
$photo_data['raw_name'] = $this->upload->data('raw_name');
$photo_data['orig_name'] = $this->upload->data('orig_name');
$photo_data['file_ext'] = $this->upload->data('file_ext');
$photo_data['file_size'] = $this->upload->data('file_size');
$photo_data['is_image'] = $this->upload->data('is_image');
$photo_data['image_width'] = $this->upload->data('image_width');
$photo_data['image_height'] = $this->upload->data('image_height');
$photo_data['image_type'] = $this->upload->data('image_type');
$photo_data['image_size_str'] = $this->upload->data('image_size_str');
$this->products_model->add_product_photo($photo_data);
}
}
然后模型函数看起来像这样。
public function add_product_photo($photo_data){
$this->db->insert('product_photos_tbl', $photo_data);
$product_photo_id = $this->db->insert_id();
return $product_photo_id;
}