发生数据库错误:1048无法插入数据

时间:2018-10-07 18:06:13

标签: php database codeigniter crud

即使我的数据库已经和代码一样,我也无法保存数据。我该怎么办?

  

发生数据库错误,错误号:1048

     

“图片”列不能为空

     

插入galleryid_gallerynameimage)值   (“ 5bba4390eb0b8”,“ nnn”,NULL)

     

文件名:C:/xampp/htdocs/eat/system/database/DB_driver.php

     

行号:691

控制器: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'));
        }
    }
}

型号: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.*"));
        }
    }

}

在视图中有3个页面:list.php,new_form.php,edit_form.php list.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") ?>

                <!-- DataTables -->
                <div class="card mb-3">
                    <div class="card-header">
                        <a href="<?php echo site_url('admin/gallery/add') ?>"><i class="fas fa-plus"></i> Add New</a>
                    </div>
                    <div class="card-body">

                        <div class="table-responsive">
                            <table class="table table-hover" id="dataTable" width="100%" cellspacing="0">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Photo</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($gallery as $gallery): ?>
                                    <tr>
                                        <td width="150">
                                            <?php echo $gallery->name ?>
                                        </td>
                                        <td>
                                            <img src="<?php echo base_url('upload/gallery/'.$gallery->image) ?>" width="64" />
                                        </td>
                                        <td width="250">
                                            <a href="<?php echo site_url('admin/gallery/edit/'.$gallery->gallery_id) ?>"
                                             class="btn btn-small"><i class="fas fa-edit"></i> Edit</a>
                                            <a onclick="deleteConfirm('<?php echo site_url('admin/gallery/delete/'.$gallery->gallery_id) ?>')"
                                             href="#!" class="btn btn-small text-danger"><i class="fas fa-trash"></i> Hapus</a>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

            </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /.content-wrapper -->

    </div>
    <!-- /#wrapper -->


    <?php $this->load->view("admin/_partials/scrolltop.php") ?>
    <?php $this->load->view("admin/_partials/modal.php") ?>

    <?php $this->load->view("admin/_partials/js.php") ?>

    <script>
    function deleteConfirm(url){
        $('#btn-delete').attr('href', url);
        $('#deleteModal').modal();
    }
    </script>
</body>

</html>

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>

0 个答案:

没有答案