【问题标题】:How to store last insert id in codeigniter session if insert is donr by AJAX?如果插入是由 AJAX 完成的,如何在 codeigniter 会话中存储最后一个插入 ID?
【发布时间】:2014-07-07 10:32:20
【问题描述】:

我需要在某些事件中插入用户的最后一个插入 ID ..... 我想在另一页上得到它.... 喜欢在产品页面上......用户自定义他的产品......喜欢mug customization

我需要将插入的....自定义产品ID添加到购物车页面,这是由ajax完成的....那么我如何从产品页面获取购物车页面上的最后一个插入ID....

我的 ajax...

$('#store').click(function () {

    $.ajax({
        type: "POST",
        url: ajax_url_store,
        data: {
            action: 'store',
            views: JSON.stringify(thsirtDesigner.getProduct())
        },

        success: function (data) {

            if (parseInt(data) > 0) {
                //successfully added

                document.getElementById('succes-message').innerHTML = 'You Design has Been SuccessFully Saved';
            } else {

            }

        },
        error: function () {
            //alert('some error has occured...');
        },
        start: function () {
            //alert('ajax has been started...');    
        }
    });
});

我的 ci 函数

public function saveData() {

    if ($this - > input - > post('action') == 'store') {

        $views = $this - > input - > post('views');
        $id = $this - > product_model - > save($views);

        if ($id != '') {
            header('Content-Type: application/json');
            echo json_encode($id);
        }
    }
}

【问题讨论】:

    标签: php jquery mysql ajax codeigniter


    【解决方案1】:
    public function saveData() 
    {
    
      if ($this->input->post('action') == 'store') 
      {
    
        $views = $this->input->post('views');
        $id = $this->product_model->save($views);
        $data = array('userid'=>$id);       //insert id to userid as session variable
        $this->session->set_userdata($data);  //set userid as a session variable
    
        if ($id != '') 
        {
            header('Content-Type: application/json');
            echo json_encode($id);
        }
      }
    }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 2017-02-06
      • 1970-01-01
      相关资源
      最近更新 更多