Я не могу сохранить свои данные, даже если моя база данных уже похожа на код ... Что мне делать ??
A Database Error Occurred Error Number: 1048
Column 'image' cannot be null
INSERT INTO gallery
(id_gallery
, name
, image
) VALUES
('5bba4390eb0b8', 'nnn', NULL)
Filename: C:/xampp/htdocs/eat/system/database/DB_driver.php
Line Number: 691
Controller: Gallery.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Gallery extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("gallery_model");
$this->load->library('form_validation');
}
public function index()
{
$data["gallery"] = $this->gallery_model->getAll();
$this->load->view("admin/gallery/list", $data);
}
public function add()
{
$gallery = $this->gallery_model;
$validation = $this->form_validation;
$validation->set_rules($gallery->rules());
if ($validation->run()) {
$gallery->save();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$this->load->view("admin/gallery/new_form");
}
public function edit($id = null)
{
if (!isset($id)) redirect('admin/gallery');
$gallery = $this->gallery_model;
$validation = $this->form_validation;
$validation->set_rules($gallery->rules());
if ($validation->run()) {
$gallery->update();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$data["gallery"] = $gallery->getById($id);
if (!$data["gallery"]) show_404();
$this->load->view("admin/gallery/edit_form", $data);
}
public function delete($id=null)
{
if (!isset($id)) show_404();
if ($this->gallery_model->delete($id)) {
redirect(site_url('admin/gallery'));
}
}
}
Model: Gallery_model.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Gallery_model extends CI_Model
{
private $_table = "gallery";
public $id_gallery;
public $name;
public $image;
public function rules()
{
return [
['field' => 'name',
'label' => 'Name',
'rules' => 'required']
];
}
public function getAll()
{
return $this->db->get($this->_table)->result();
}
public function getById($id)
{
return $this->db->get_where($this->_table, ["id_gallery" => $id])->row();
}
public function save()
{
$post = $this->input->post();
$this->id_gallery = uniqid();
$this->name = $post["name"];
$this->image = $this->_uploadImage();
$this->db->insert($this->_table, $this);
}
public function update()
{
$post = $this->input->post();
$this->id_gallery = $post["id"];
$this->name = $post["name"];
if (!empty($_FILES["image"]["name"])) {
$this->image = $this->_uploadImage();
} else {
$this->image = $post["old_image"];
}
$this->db->update($this->_table, $this, array('id_gallery' => $post['id']));
}
public function delete($id)
{
$this->_deleteImage($id);
return $this->db->delete($this->_table, array("id_gallery" => $id));
}
private function _uploadImage()
{
$config['upload_path'] = './upload/gallery/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = $this->id_gallery;
$config['overwrite'] = true;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
}
private function _deleteImage($id)
{
$gallery = $this->getById($id);
if ($gallery->image != "default.jpg") {
$filename = explode(".", $gallery->image)[0];
return array_map('unlink', glob(FCPATH."upload/gallery/$filename.*"));
}
}
}
In view there is 3 page: list.php, new_form.php, edit_form.php
list.php
<!DOCTYPE html>
load-> view ("admin / _partials / head.php")?> load-> view ("admin / _partials / navbar.php")?>load-> view ("admin / _partials / sidebar.php")?>load-> view ("admin / _partials / breadcrumb.php")?>"> Добавить новый Имя Фото Действие имя?>
image) ?>" width="64" />
gallery_id) ?>"
class="btn btn-small"> Редактировать Hapus load-> view ("admin / _partials / scrolltop.php")?>load-> view ("admin / _partials / modal.php")?>load-> view ("admin / _partials / js.php")?> * функция deleteConfirm (url) {$ ('# btn-delete'). attr ('href', url);$ ( '# deleteModal') модальный ().}
new_form.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("admin/_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("admin/_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("admin/_partials/breadcrumb.php") ?>
<?php if ($this->session->flashdata('success')): ?>
<div class="alert alert-success" role="alert">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/gallery/') ?>"><i class="fas fa-arrow-left"></i> Back</a>
</div>
<div class="card-body">
<form action="<?php base_url('admin/gallery/add') ?>" method="post" enctype="multipart/form-data" >
<div class="form-group">
<label for="name">Title*</label>
<input class="form-control <?php echo form_error('name') ? 'is-invalid':'' ?>"
type="text" name="name" placeholder="gallery name" />
<div class="invalid-feedback">
<?php echo form_error('name') ?>
</div>
</div>
<div class="form-group">
<label for="image">Photo</label>
<input class="form-control-file <?php echo form_error('image') ? 'is-invalid':'' ?>"
type="file" name="image"/>
<div class="invalid-feedback">
<?php echo form_error('image') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
<div class="card-footer small text-muted">
* required fields
</div>
</div>
</div>
</div>
</div>
<?php $this->load->view("admin/_partials/scrolltop.php") ?>
<?php $this->load->view("admin/_partials/js.php") ?>
</body>
</html>
edit_form.php
<!DOCTYPE html>
<html lang="en">
<head>
<?php $this->load->view("admin/_partials/head.php") ?>
</head>
<body id="page-top">
<?php $this->load->view("admin/_partials/navbar.php") ?>
<div id="wrapper">
<?php $this->load->view("admin/_partials/sidebar.php") ?>
<div id="content-wrapper">
<div class="container-fluid">
<?php $this->load->view("admin/_partials/breadcrumb.php") ?>
<?php if ($this->session->flashdata('success')): ?>
<div class="alert alert-success" role="alert">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php endif; ?>
<!-- Card -->
<div class="card mb-3">
<div class="card-header">
<a href="<?php echo site_url('admin/gallerys/') ?>"><i class="fas fa-arrow-left"></i>
Back</a>
</div>
<div class="card-body">
<form action="<?php base_url(" admin/gallery/edit") ?>" method="post"
enctype="multipart/form-data" >
<input type="hidden" name="id" value="<?php echo $gallery->gallery_id?>" />
<div class="form-group">
<label for="name">Name*</label>
<input class="form-control <?php echo form_error('name') ? 'is-invalid':'' ?>"
type="text" name="name" placeholder="gallery name" value="<?php echo $gallery->name ?>" />
<div class="invalid-feedback">
<?php echo form_error('name') ?>
</div>
</div>
<div class="form-group">
<label for="name">Photo</label>
<input class="form-control-file <?php echo form_error('image') ? 'is-invalid':'' ?>"
type="file" name="image" />
<input type="hidden" name="old_image" value="<?php echo $gallery->image ?>" />
<div class="invalid-feedback">
<?php echo form_error('image') ?>
</div>
</div>
<input class="btn btn-success" type="submit" name="btn" value="Save" />
</form>
</div>
<div class="card-footer small text-muted">
* required fields
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content-wrapper -->
</div>
<!-- /#wrapper -->
<?php $this->load->view("admin/_partials/scrolltop.php") ?>
<?php $this->load->view("admin/_partials/js.php") ?>
</body>
</html>