发生数据库错误,错误号:1062键“ PRIMARY”的条目“ 5”重复

时间:2018-10-10 04:47:26

标签: php database codeigniter crud

每次添加数据时,它总是从ID号5开始,因此当我添加其他数据时,它会出错...有人可以帮助我吗?

  

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

     

键“ PRIMARY”的条目“ 5”重复

     

插入galleryid_gallerynameimage)值   (“ 5bbd81467f388”,“牛排”,“ IMG_1232.JPG”)

     

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

     

行号: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'));
        }
    }
}

型号:Gallery_model.php

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/galery/';
        $config['allowed_types']        = 'gif|jpg|png|jpeg';
        $config['upload_max_filesize']  = '100000M';
        $config['post_max_size']        = '100000M';
        $config['file_name']            = basename($_FILES["image"]["name"]);
        $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/galery/$filename.*"));
        }
    }

}

Views:new_form.php

<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('name') ? '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>

1 个答案:

答案 0 :(得分:0)

如果您使用Integer,请尝试将 id_gallery 的类型字段从int-> PRIMARY更改为varchar-> PRIMARY,并且不要在数据库上使用自动增量。 对我有用:3