0

Currently in my database I have a column called refno which has the format of LP-C-1024 where 1024 has to be an auto incrementing number while the LP-C- remains same. Now I want my query to reference the last record in my database and increment the number by +1 and have it so that when I include my insert statement, it should reference this value.

My current insert statement:

Controller Class:

$maindata=array('clients_id'=>$this->session->userdata('clientsessid'),
'agents_id'=>$this->security->xss_clean($this->input->post('agents_id')),
'refno'=>$this->security->xss_clean($this->input->post('refno')),
'title'=>$this->security->xss_clean($this->input->post('title'));
$insertid=$this->contacts_model->insert($maindata);

Model Class:

function insert($maindata)
    {
        $this->db->insert("crm_contacts",$maindata);
        $prime=$this->db->insert_id();
        return $prime;
    }
H2O
  • 313
  • 2
  • 9
  • If you want your database to generate the unique reference then you have to insert a record, otherwise as you have noted you could easily end up with two records having the same 'unique' reference. If you want your code to generate the unique reference then you should look at using [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier). A workaround for the first problem could be to insert a record then delete it if it is no longer needed, although this is slightly inefficient and could lead to holes in your refno generation (which may or may not be a problem). – mark_b Nov 10 '21 at 11:55
  • Is the `LP-C-` part static and no other prefixes needed? If so, just set up a normal autoincrement field and concatenate `LP-C-` in front of it when you display the values or have a generated field that concatenates the autoincremented id with the prefix. – Shadow Nov 10 '21 at 12:01

0 Answers0