【问题标题】:CodeIgniter inserting auto increment [duplicate]CodeIgniter 插入自动增量
【发布时间】:2016-08-17 12:02:34
【问题描述】:

我正在为工作做一个基本的添加用户表单,我正在尝试将一个用户添加到数据库中,但是当我尝试时,我只是得到主键被重复错误。很确定解决方案正对着我,但我今天碰壁了,哈哈。

数据库表结构

dbo.ci_users

id(PK, int, not null)
user_name(nchar255, not null)
user_email(nchar255, not null)
user_password(nchar255, not null)
user_displayname(nchar255, not null)
user_active(smallint, not null)
user_level(smallint, not null)

添加用户模型

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class adduser_database extends CI_Model {

         function __construct()
        {
            // Call the Model constructor
            parent::__construct();
            $this->load->database();
        }

    public function insert_into_db()
    {
        $data = array(
            'id'            => '0',
            'user_active'        => '1',
            'user_level'         => '2',
            'user_displayname'   => $this->input->post('user_displayname'),
            'user_email'         => $this->input->post('user_email'),
            'user_name'      => $this->input->post('user_name'),
            'user_password'      => $this->input->post('user_password') 
        );

        $this->db->insert('ci_users', $data);

    }
}

【问题讨论】:

  • 是的,我试过了,但我现在收到此错误无法将值 NULL 插入列 'id',表 'ClientPortal.dbo.ci_users';列不允许空值。插入失败。
  • 如果 id 是 auto_increment 从数组中删除 'id' => '0',然后尝试
  • @Piglet 添加此 ALTER TABLE ci_users MODIFY id int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
  • 会影响他数据库中已经存在的任何值吗?因为我们已经有 20 个用户在那里,只是想知道这个更改是否会删除他们?
  • @Piglet 不会。不会影响你以前的记录。

标签: php mysql codeigniter


【解决方案1】:

您正在为所有插入指定一个相同的主键值。这就是您收到该错误的原因。删除那行代码,让 MySQL 为您指定该值作为 AUTO_INCREMENT 意味着 MySQL 将在每次插入时自动分配下一个值:

$data = array(
    'id'          => '0', // <-- REMOVE THIS
    'user_active' => '1',

编辑

您似乎忘记将 AUTO_INCREMENT 添加到您的表中。这段代码解决了这个问题:

ALTER TABLE ci_users CHANGE id id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY;

【讨论】:

  • 感谢您的回复,删除后我收到此错误,无法将值 NULL 插入列 'id',表 'ClientPortal.dbo.ci_users';列不允许空值。插入失败。
  • @Piglet 我已经更新了我的答案,向您展示如何将自动增量添加到您的表格中
  • 我的意思是这就是我现在得到的消息 102,级别 15,状态 1,第 1 行 'CHANGE' 附近的语法不正确。
【解决方案2】:
First of all you have to change in database and make AUTO_INCREMENT id then use the following code. It will helps you 

public function insert_into_db()
{
    $data = array(

        'user_active'        => '1',
        'user_level'         => '2',
        'user_displayname'   => $this->input->post('user_displayname'),
        'user_email'         => $this->input->post('user_email'),
        'user_name'      => $this->input->post('user_name'),
        'user_password'      => $this->input->post('user_password') 
    );

    $this->db->insert('ci_users', $data);

【讨论】:

    【解决方案3】:

    如果您使用表中的模型定义,请务必更改布尔参数:

    protected $useAutoIncrement = true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多