duaj39673 2014-01-28 14:07
浏览 43
已采纳

最后插入id /最新插入的id函数codeigniter

Codes:

View

<html>
<body>
<?php echo form_open('samplecontroller/sample'); ?>
<input type="text" name="samplename"/> 
<input type="submit" value="go"/> 
<?php echo form_close(); ?>
</body>
</html>

Controller

<?php

Class samplecontroller extends CI_Controller
{

    public function __construct() 
    {
    parent::__construct();
    $this->load->model('Sample_insert');
    }

    function index()
    {
        $this->load->view('sample.php');

    }

    function show($data)
    {
        $this->load->view('sampleresult',$data);
    }

    function sample()
    {
            if($result = $this->Sample_insert->insert_into_sample())
            {
             $this->show($result);
            }

    }
}
?>

Model

<?php
Class Sample_insert extends CI_Model
{
    function insert_into_sample()
    {
        $sample = array(
            'samplename' => $this->input->post('samplename'),
            );      

        $this->db->insert('sample', $sample);

        $latest_id = $this->db->insert_id();

        return $latest_id;
    }
}
?>

The flow is that, i have 2 views. The other one contains only 1 text input, after which goes to controller then controller passes it on to model then model gets the value within the text input then inserts to my sample table.

The sample table has 2 columns, id(PK,AI) and name

after it inserts, on my model. i added a line, return $latest_id. Then passes goes back to my controller then passes it off to another view that only has

print_r($data);

After all that, it displays an error.

Message: Undefined variable: data

Im not sure where the flow went wrong or if i made a syntax mistake. If someone knows/expert on insert_id(), could you pin point where exactly i am wrong? anyways, ive made my research and been getting results on the web how buggy insert_id is. Im not sure if its true or not but ive been redirected mostly to forums wherein insert_id returns null and some say its a bug. Im hoping it isnt.

  • 写回答

1条回答 默认 最新

  • doujiu8826 2014-01-28 14:54
    关注

    As per your comment, You have to do some changes in your controller.

    function show($data)
        {
            $this->load->view('sampleresult',array("data"=>$data));
        }
    

    You can get inserted id in view in the name of $data.

    For ex:

    echo $data;
    

    Edit: As per your comment in answer, For multiple row insert in model, add below code

    $id_arr = array();
    foreach($sample_data as $sample)
    {
    
       $this->db->insert('sample', $sample);
       $id_arr[] = $this->db->insert_id();
    }
    return $id_arr;
    

    Now in your view, you will get ids in $data

    foreach($data as $id)
    {
       echo $id;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?