Welcome Guest, Not a member yet? Register   Sign In
Function $this->db->insert_id() to get last inserted record id always returns 1
#1

[eluser]Andy UK[/eluser]
Hi,

I'm developing a ticket system where tickets are assigned to relationships with clients. If a relationship exists, then the ticket is given that relationship id. If no current relationship exists then a new one is added and the new id is assigned to the ticket that triggered its creation. The problem i have is that when i get the last insert id of the new relationship and add it to the recently created ticket, i'm always getting the value of 1. Here is the controller code...

Code:
//Insert new ticket into system
  $ticket_data = array(
        'contact_name'   => $name,
        'contact_email'   => $email,
        'contact_number'  => $contact_number,
        'ticket_body'   => $notes,
        'property_id'   => $property_id,
        'ticket_type'   => 'Viewing Request',
        'ticket_source'   => 'Website',
        'assigned_to'   => $broker_id,
        'ticket_creation_date' => date("Y-m-d H:i:s")
       );
  
  // Add new ticket to database and get its ID
  $ticket_id = $this->property->addNewTicket($ticket_data);
  
  // Check for existing open relationship with client via email address or phone number
  $relationship_id_via_email = $this->property->relationshipCheckEmail($email);
  $relationship_id_via_phone = $this->property->relationshipCheckPhone($contact_number);
  if($relationship_id_via_email)
  {
   $this->property->assignTicketRelationship($ticket_id, $relationship_id_via_email);
  }
  else
  // Else if no relationship exists, add a new one
  {
   $relationship_data = array(
           'name'    => $name,
           'email'    => $email,
           'tel'    => $contact_number,
           'status_id'   => 1,
           'client_source_id' => 1,
           'added_on'   => date("Y-m-d H:i:s")
   );
   $new_relationship_id = $this->property->openNewRelationship($relationship_data);
  
   // Assign the new relationship ID to the previously added ticket
   $this->property->assignTicketRelationship($ticket_id, $new_relationship_id);
  }

And here is the relevant model code...

Code:
function addNewTicket($data)
{
  $this->db->insert('tickets', $data);
  
  return $this->db->insert_id();
}

Code:
function openNewRelationship($data)
{
  $this->db->insert('client_relationship_instances', $data);
  
  return $this->db->insert_id();
}

$new_relationship_id is the variable that holds the last inserted client relationship and that's the one that is coming back as 1 every time a new client relationship is created.

Any ideas?
#2

[eluser]Andy UK[/eluser]
FYI, I'm currently using Codeigniter version 2.0.1. I'll try upgrading to 2.1.4 to see if that helps...
#3

[eluser]Andy UK[/eluser]
Upgraded to 2.1.4 and problem persists. Any help is much appreciated!
#4

[eluser]Andy UK[/eluser]
For reasons unbeknownst to me, it is now working. The only change i made was switching from this

Code:
return $this->db->insert_id();

To this...

Code:
return mysql_insert_id();

Not strange in itself. But when I switched back again the first option worked too!

No idea what's going on there, but it's working now.




Theme © iAndrew 2016 - Forum software by © MyBB