使用codeigniter将数据保存到数据库中时获取id

时间:2014-06-23 14:50:00

标签: codeigniter

嗨我保存时将此表格保存到数据库中。我希望当数据保存到数据库中时,我会在其上获取id,然后将其显示到下一页。 这是我的函数add_new

中的控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Create_album extends CI_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->library('session');
    $this->load->model('admin_model', 'am');
    $this->load->library('form_validation');
    if(!$this->session->userdata('logged_in')){
      redirect('login');
    }
  }

  public function detail($id){
    return $id;
    $this->data['item'] = $this->am->getItem($id);
    print_r($this->data['item']);exit;
  }

  public function add_new(){
    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('description', 'Description', 'required');
    if($this->form_validation->run() == FALSE){
      $this->data['title'] = 'Create New Album';
      $this->data['logout'] = 'logout';
      $this->data['home'] = 'activities';

      $session_data = $this->session->userdata('logged_in');
      $this->data['id'] = $session_data['id'];
      $this->data['username'] = $session_data['username'];

      $this->load->view('pages/admin_header', $this->data);
       $this->load->view('content/create_album', $this->data);
      $this->load->view('pages/admin_footer');
    }else{
      $array = array(
                'title'=>$this->input->post('title'),
                'description'=>$this->input->post('description')
                );
      $this->am->saveAlbum($array);
      $id = $this->db->id;
      $this->data['item'] = $this->am->getItem($id);
      return $this->am->saveAlbum($id);
      foreach($this->data['item'] as $item){
        $itemId = $item->id;

      }
      return $itemId;
      redirect('create_album/detail/id/'.$itemId);
    }
  }

  public function index(){
    $this->data['title'] = 'Create Album';
    $this->data['logout'] = 'logout';
    $this->data['home'] = 'activities';

    $session_data = $this->session->userdata('logged_in');
    $this->data['id'] = $session_data['id'];
    $this->data['username'] = $session_data['username'];

    $this->load->view('pages/admin_header', $this->data);
    $this->load->view('content/create_album', $this->data);
    $this->load->view('pages/admin_footer');
  }

}

我的模特

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Admin_model extends CI_Model{
  public function getItem($id){
    return $this->db->select('item.id,
                         item.parent_id,
                         item.title,
                         item.description,
                         item.filename
                        '
                        )
                 ->from('item')
                 ->where('item.id', $id)
                 ->get()->result_object();
     $this->db->get('item');

  }


}

&GT;

有人可以帮我解决这个问题吗?我希望在保存数据时获取ID。任何帮助都非常感谢。谢谢

3 个答案:

答案 0 :(得分:0)

认为您可能正在寻找insert_id()查询辅助函数。您可以在Codeigniter docs

中查看有关该信息的信息

答案 1 :(得分:0)

只需使用此

即可
$this->db->insert_id(); // Returns your row id.

这里你的控制器应该是什么样子

public function add_new(){
    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('description', 'Description', 'required');
    if($this->form_validation->run() == FALSE){
      $this->data['title'] = 'Create New Album';
      $this->data['logout'] = 'logout';
      $this->data['home'] = 'activities';

      $session_data = $this->session->userdata('logged_in');
      $this->data['id'] = $session_data['id'];
      $this->data['username'] = $session_data['username'];

      $this->load->view('pages/admin_header', $this->data);
       $this->load->view('content/create_album', $this->data);
      $this->load->view('pages/admin_footer');
    }else{
      $array = array(
                'title'=>$this->input->post('title'),
                'description'=>$this->input->post('description')
                );
      $this->am->saveAlbum($array);
      $id = $this->db->id;
      $this->data['item'] = $this->am->getItem($id);
      return $this->am->saveAlbum($id);
      foreach($this->data['item'] as $item){
        $itemId = $item->id;

      }
      return $itemId;
      redirect('create_album/detail/id/'.$this->db->insert_id()); // here?
    }
  }

答案 2 :(得分:0)

当PHP在函数中命中return时,它会执行此操作,返回一个值,然后退出该函数。返回后的代码将不会被执行。阅读它on the docs page

示例:

public function detail($id){
  return $id;
  echo 'here';
}

由于您已在function()中返回了一个值,因此您永远不会在此处“回复”。

同样,这会在您的代码中应用两次,一次在此处:

return $itemId;
redirect('create_album/detail/id/'.$this->db->insert_id());

再来一次:

$this->data['item'] = $this->am->getItem($id);
return $this->am->saveAlbum($id);

如果您想要插入ID,则必须从$this->am->saveAlbum();返回它,将其分配给变量并将其传递给重定向。

还有很多其他问题,但这应该有助于您入门。