Welcome Guest, Not a member yet? Register   Sign In
Having trouble with $this->db->insert_id()
#1

[eluser]Chad Crowell[/eluser]
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:

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?
#2

[eluser]JayTee[/eluser]
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?
#3

[eluser]TheFuzzy0ne[/eluser]
Check that the primary key column has the auto increment attribute set.
#4

[eluser]Chad Crowell[/eluser]
@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
#5

[eluser]TheFuzzy0ne[/eluser]
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.
#6

[eluser]Chad Crowell[/eluser]
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.

Code:
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');    
                    
                }
                
            }
            
        }
        
    }
#7

[eluser]misteryosox44[/eluser]
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.
#8

[eluser]CroNiX[/eluser]
Why is everyone responding to 3 year old posts tonight? lol
#9

[eluser]misteryosox44[/eluser]
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)




Theme © iAndrew 2016 - Forum software by © MyBB