我需要得到最后插入id从控制器到ajax成功函数......我想自定义产品,当用户按添加到购物车ajax被执行和设计是要插入数据库....在插入数据库后,我得到最后插入id.....插入…但需要最后插入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..here i am getting last insert id...
// and i want to pass that in hidden variable.....
// on view page.....
}
else {
document.getElementById('succes-message').innerHTML = 'You Design has not Been Saved';
}
},
error: function() {
//alert('some error has occured...');
},
start: function() {
//alert('ajax has been started...');
}
});
});
my CI controller
public function saveData(){
if($this->input->post('action') == 'store') {
$views = $this->input->post('views');
$id = $this->product_model->save($views);
$data = array('cust_id'=>$id);
$this->session->set_userdata($data);
if($id !=''){
header('Content-Type: application/json');
echo json_encode($id);
}
}
}
$this->db->insert_id()返回最后一个id。
在模型中插入操作后,使用以下代码
$ this -> db -> insert_id ();
这将给你最后一次插入的id
。