简体   繁体   中英

PHP Grocery Crud Last Inserted Id not found

i want to redirect the page after save to a new page which must have the last inserted id(primary key)

            $crud = new grocery_CRUD();
        //  $crud->set_theme('datatables');
        $crud->set_table('visitor');
        $crud->set_subject('Visitor');
        $crud->required_fields('id');            
        $crud->columns('event_type', 'name', 'number', 'email', 'company_name', 'designation');
        $crud->set_relation('event_type', 'event_type', 'event');
        $crud->field_type('visitor_type', 'dropdown', array('1' => 'Visitor', '2' => 'Exhibitor', '3' => 'Staff'));
        $crud->callback_before_insert(array($this, 'insert_data_callback'));
        $crud->callback_after_insert(array($this, 'after_insert'));
        $crud->change_field_type('date', 'invisible');
        $crud->set_lang_string('insert_success_message',
     'Your data has been successfully stored into the database.<br/>Please wait while you are redirecting to the list page.
     <script type="text/javascript">
      window.location = "'.site_url('visitor').'/'.'barcode/'.$this->db->insert_id().'";
     </script>
     <div style="display:none">
     '
        ); 

where window.location = "'.site_url('visitor').'/'.'barcode/'.$this->db->insert_id().'"; is the main thing. the $this->db->insert_id() always returns 0 i also tried using the callback_after_insert function and added the following code, however this too return 0 always

function after_insert($post_array, $lastInsertedId) {
    print_r($post_array);
    echo " hello this is called ". $lastInsertedId. " ". $this->db->insert_id();
}

but this doesn't seem to work, i just want the last inserted id and redirect it to a new page. i have surfed grocery crud site and stackoverflow for solution to this and tried almost every possible option but nothing seems to work for this.

try this:

$query = $this->db->query('SELECT LAST_INSERT_ID()');
$row = $query->row_array();

then access the id with

$row['LAST_INSERT_ID()'];

On your controller:

 $crud->set_lang_string('insert_success_message',
            'Your data has been successfully stored into the database.   <br/>Please wait while you are redirecting to the list page.
            <script type="text/javascript">
            window.location = "'.site_url(strtolower(__CLASS__).'/redirect_to_url/').'";
            </script>
            <div style="display:none">
        '
        );
    $crud->callback_after_insert(array($this,'_something_after_insert'));

then a callback after insert adding your insert_id to flash data

function _something_after_insert($post_array, $primary_key) 
    {
        $this->session->set_flashdata('last_pk', $primary_key);
        return true;
    }

then your method to redirect

function redirect_to_url() 
{
    redirect(base_url('something/edit/'.$this->session->flashdata('last_pk')),'refresh');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM