【问题标题】:A Database Error Occurred Error Number: 1062 Duplicate entry '5' for key 'PRIMARY'发生数据库错误错误号:1062 键 'PRIMARY' 的重复条目 '5'
【发布时间】:2019-03-14 22:13:57
【问题描述】:

每次我添加数据时,它总是从 id 编号 5 开始,所以当我添加另一个数据时,它会出错...有人可以帮助我吗?

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

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

插入gallery(id_gallery,name,image)值 ('5bbd81467f388', '牛排', 'IMG_1232.JPG')

文件名: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

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

}

视图: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>

【问题讨论】:

  • 确保此 ID 的值是唯一的且不重复:id_gallery
  • 什么意思?你能给我举个例子吗? @萨拉赫
  • $this->id_gallery = uniqid();为什么在 Gallery_model.php 中注释了这一行?
  • 哦..我现在明白了,我在教程上看到了
  • db 中的主键是唯一的。检查该 id 是如何由您的模型创建并传递给 save 方法的。如果您不想要唯一键,请更改您的数据库

标签: php database codeigniter crud


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-02-02
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2017-10-09
    • 1970-01-01
    相关资源
    最近更新 更多