EllisLab text mark
Advanced Search
     
Having trouble with $this->db->insert_id()
Posted: 01 March 2009 11:51 PM
Avatar
Joined: 2007-06-24
330 posts

I have a working php/mssql/odbc CI install on 1.7.1.  I’m just in the beginnings of a new app.

I’ve got this function in my controller that inserts a kit title into the db table and *should* return the unique ID from the table and allow me to redirect the user to the edit page for this new kit.

Here is the code:

function add() {
        
        
//grab the post
        
$kitname $this->input->post('kitname');
        
        if (
$kitname != false{
            
            
//put the kit into the db
            
$data = array(
                    
'kitname'     => $kitname
                    
);
                    
            
//perform the insert
            
$this->db->insert('kits'$data);
            
//get the new kit ID
            
$kit_id $this->db->insert_id();
            
            if (
$kit_id//insert was successful
                
                
echo $kit_id;

                
//send the user to the edit form
                
redirect('kits/edit/'.$kit_id'refresh');

            
}
            
        }
        
    } 

The insert is happening with no problems - I can see the new kit every time I test this when I go back to the kit listing page, but I am not seeing a $kit_id echoe’d to the add page and I am not being forwarded to the edit page (I realize I don’t need a physical add page but i was using it for testing).

I did have to chase down a couple of bugs for the odbc driver for mssql but its been working great.  Maybe related, though?

 Signature 

Chad Crowell | Clearfire, Inc. | @chadcrowell | @clearfire

 
Posted: 02 March 2009 12:52 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-03-19
122 posts

Even when you comment out the redirect, you’re not seeing the id, right?

Have you tried just using the “SELECT @@IDENTITY” to bypass the CI active-record class to see if you can even get it back?

 Signature 

I plan on being spontaneous tomorrow.

 
Posted: 02 March 2009 06:09 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

Check that the primary key column has the auto increment attribute set.

 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 02 March 2009 10:59 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-24
330 posts

@JayTee Yes, without the redirect, nothing shows on the page.  Thanks will try that- though I’d love to remain in AR if I can

@TheFuzzyOne - yes that is set.  This is a pre-existing ASP site that I am converting to CI

 Signature 

Chad Crowell | Clearfire, Inc. | @chadcrowell | @clearfire

 
Posted: 02 March 2009 11:04 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

I know this sounds obvious, but can you confirm that your if statement is actually being hit? I once forgot that I added an underscore to the name attribute in the view, and spent ages trying to figure out what was wrong. Of course, now it’s the first place I look when I get problems similar to this.

 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 02 March 2009 11:49 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-24
330 posts

I found some info on the web that the odbc_insert_id command from PHP doesn’t work too well, so I decided not to fight it and went with this, which works fine as this is a low transaction site.

function add() {
        
        
//grab the post
        
$kitname $this->input->post('kitname');
        
        if (
$kitname != false{
            
            
//put the kit into the db
            
$data = array(
                    
'kitname'     => $kitname,
                    
'active'    => 0
                    
);
                    
            
//perform the insert
            
$result $this->db->insert('kits'$data);
            
            if (
$result//insert was successful
                
                //we need to get that kit id now
                
$this->db->select('id')->from('kits')->order_by('id','desc')->limit('1');
                
$query $this->db->get();
                
                if (
$query->num_rows() > 0{
                    
                    $row 
$query->row();
                    
                    
$kit_id $row->id;
                    
                    
//send the user to the edit form
                    
redirect('kits/edit/'.$kit_id'refresh');    
                    
                
}
                
            }
            
        }
        
    } 
 Signature 

Chad Crowell | Clearfire, Inc. | @chadcrowell | @clearfire

 
Posted: 18 April 2012 11:04 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2012-04-18
3 posts

If this still helps, you can also use this method for getting the insert_id;

$res = $this->db->query(“SELECT @@IDENTITY as insert_id”)->row_array();
echo $res[‘insert_id’]; //the last inserted id

//you may place it right after your insert statement.

 Signature 

Wisdom starts from wonder.

 
Posted: 19 April 2012 01:03 AM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
4539 posts

Why is everyone responding to 3 year old posts tonight?  lol

 Signature 
 
Posted: 19 April 2012 01:07 AM   [ # 8 ]   [ Rating: 0 ]
Joined: 2012-04-18
3 posts

hahaha. sorry for that , just new in this forum, and experienced the same problem. Newbie programmer here smile (just trying to help for someone like me)

 Signature 

Wisdom starts from wonder.