简体   繁体   English

找不到PHP Grocery Crud最后插入的ID

[英]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) 我想在保存到新页面后重定向页面,该页面必须具有最后插入的id(主键)

            $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().'"; 其中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 $this->db->insert_id()始终返回0,我也尝试使用callback_after_insert函数并添加了以下代码,但是此方法也总是返回0

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. 但这似乎不起作用,我只想要最后插入的ID并将其重定向到新页面。 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. 我已经浏览了杂货杂货网站和stackoverflow来解决此问题,并尝试了几乎所有可能的选择,但似乎对此没有任何效果。

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 然后在插入后的回调将您的insert_id添加到Flash数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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