gpt4 book ai didi

php - 如何在codeigniter中使用模态上传图像并插入数据库

转载 作者:行者123 更新时间:2023-11-29 07:26:01 25 4
gpt4 key购买 nike

已解决,请阅读下面我的评论

所以我想在codeigniter中使用模态上传图像,答案就在我眼前,就在这个链接https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html上,但问题是,在该网络上,它只显示不使用任何模式的操作表单上传,也不将其插入任何数据库,我已经尝试过,但因为我不擅长编码,更不用说刚刚学会了如何像昨天一样使用codeigniter,所以我几乎不明白如何在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">&times;</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-->

下面是我得到的错误,我相信这是因为查询或函数不知何故我不知道如何使用它。

Error Number: 1048

Column 'foto' cannot be null

INSERT INTO barang (nama, harga, stock, idkategori, foto) VALUES ('Meja Baru', '10000', '5', '2', NULL)

Filename: C:/xampp/htdocs/PWI/tubes/AKS-master/application/models/Crud.php

Line Number: 11

最佳答案

这就是我处理上传过程的方式。但我认为你的问题在于你的MYSQL表设置。检查照片字段是否设置了允许空设置。您可能无法将 foto 值设置为 null,并且数据库会抛出您收到的错误。

Controller 功能:

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;
}

关于php - 如何在codeigniter中使用模态上传图像并插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34531895/

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