Codeigniter 2.1项目。 错误重复条目''为密钥'PRIMARY'

I'm new to codeigniter framework. I have setup a Codeigniter v2 existing projects from github and the last couple of days I have had problems with the session.php file. All th values in the session variables are empty and I get the following error

A Database Error Occurred
Error Number: 1062

Duplicate entry '' for key 'PRIMARY'

INSERT INTO `stream_ci_sessions` (`session_id`, `ip_address`, `user_agent`, `last_activity`, `user_data`) VALUES ('', '', '', 1537445113, '')

Filename: libraries/Session.php

Line Number: 283

Session create part

function sess_create()
    {
        $sessid = '';
        while (strlen($sessid) < 32)
        {
            $sessid .= mt_rand(0, mt_getrandmax());
        }
        // To make the session ID even more secure we'll combine it with the user's IP
        $sessid .= $this->CI->input->ip_address();
        $this->userdata = array(
                            'session_id'    => md5(uniqid($sessid, TRUE)),
                            'ip_address'    => $this->CI->input->ip_address(),
                            'user_agent'    => substr($this->CI->input->user_agent(), 0, 120),
                            'last_activity' => $this->now,
                            'user_data'     => ''
                            );


        // Save the data to the DB if needed
        if ($this->sess_use_database === TRUE)
        {
            $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
        }
        // Write the cookie
        $this->_set_cookie();
    }

Anyone who has been able to resolve such an error? Thanks

Your column session_id is the primary key of your table and you inserted an empty value in it once before so the second time it forbade you because the primary key must be unique.

  • so you should add id column into your stream_ci_sessions table then make it as a primary key and auto increment.

  • You can add id and make it as a primary key and auto increment from the beginning but you first you should truncate your data.

Add an id column as a primary-key and make it auto increment.