2016-09-28 85 views
1

请帮助我如何从数据库中使用codeigniter和jquery获取最后插入的id。我想要的是插入后得到最后插入的ID。我正在使用jquery插入数据。我不知道如何去做。提前致谢。如何使用codeigniter和jquery插入后得到最后插入的ID

SCRIPT

$('#add-tag').click(function() { 
    var lang = $('#lang').val(); 
    var tag = $('#tag').val(); 

    var data = { 
     tag: tag, 
     lang: lang 
    } 

    if (tag === '') { 
     $('.error').html('<h4><i class="glyphicon glyphicon-info-sign">' + 
      '</i> Field cannot be empty!</h4>').addClass('bounceIn').show(); 

     $('.error').delay(3000).fadeOut(); 
    } else { 
     $.ajax({ 
      url: "<?= site_url('blog/add_tag'); ?>", 
      type: 'POST', 
      data: data, 
      success: function (result) { 
       //display message if successful 
       $('.message').html('<h4><i class="glyphicon glyphicon-ok">' + 
        '</i> Tag has been added</h4>').addClass('bounceIn').show(); 
       $('.message').delay(3000).fadeOut(); 
       $('#tag').val(''); 

       $('#tags').append('<a class="tags animated fadeInDown">' + 
        '<span class="remove-tag" id="remove-tag' + lid + '">' + 
        '</span></span> ' + tag + '</a>'); 

       window.setTimeout(function() { 
        location.reload(); 
       }, 2000); 
      } 
     }); 
    } 
}); 

VIEW

<div class="row"> 
    <div class="col-md-12 col-sm-12 col-xs-12"> 
     <div id="add-tag-form" class="display-none relative"> 
      <div class="well well-sm"> 
       <div class="row"> 
        <div class="col-md-12 col-sm-12 col-xs-12"> 
         <h5>Add Tag</h5> 
         <input type="text" id="tagLastID" class="form-control" placeholder="Tag Last ID" readonly> 
         <input type="hidden" id="lang" class="form-control" placeholder="Lang" required value="<?= $lang; ?>"> 
         <input type="text" id="tag" class="form-control" placeholder="Tag" required> 
         <br /> 
         <button id="add-tag" class="btn col-md-12 col-sm-12 col-xs-12">Add Tag</button> 
         <div class="text-center"><a id="close-tag">cancel</a></div> 
        </div> 
       </div> 
      </div> 
     </div> 
     <button id="add-tag-btn" class="btn col-md-12 col-sm-12 col-xs-12">Add Tag</button> 

    </div> 
</div> 

控制器

public function add_tag() { 
    $this->Mblog->addTag(); 
} 

MODEL

public function addTag() { 
    $lang = $this->input->post('lang'); 
    $tag = $this->input->post('tag'); 

    $data = array(
     'tags' => $tag, 
     'lang' => $lang 
    ); 

    $this->blog->insert('tags', $data); 

    return $this->blog->insert_id(); 
} 
+0

你有MySQL的LAST_INSERT_ID()函数 – vamsi

+0

@Drew你可以给同一个问题的链接?谢谢。 – johnlopev

+0

你也可以这样做mysql_query(“INSERT INTO testtable(1,2,3,'some')”); $ last_inserted_id = mysql_insert_id(); – vamsi

回答

1

添加回您的控制器功能或呼应的结果类似如下:

public function add_tag() { 
    return $this->Mblog->addTag(); 
} 

OR

public function add_tag() { 
    echo json_encode(['data' => $this->Mblog->addTag()]); 

    exit; 
} 

,然后尝试修改转储,看到了阿贾克斯成功响应:

$.ajax({ 
    url: "<?= site_url('blog/add_tag'); ?>", 
    type: 'POST', 
    data: data, 
    dataType: 'json', // this will convert your results to json by default 
    success: function (result) { 
     // You should get the json result 
     console.log(result.data); 

     // Your regular logic to handle result 
    }, 
    fail: function(res) { 
     console.log(res); // will log fail results 
    } 
}); 

试试这些,让我们知道这是否工作为你或不。

+0

它现在适合我!非常感谢! :) – johnlopev

+0

很高兴帮助:)感谢您将它标记为正确的答案。 – Samundra

1

如果你问的是如何从控制器将数据返回到jQuery的,然后是的,这就是答案。

与此

public function add_tag() { 
    $insert_id = $this->Mblog->addTag(); 
    echo(json_encode(array("insert_id"=>$insert_id))); 
} 

的echo'ed结果替换控制器代码将出现在您的jQuery.ajax的成功。

1

您已经从模型中返回id,因此在您的控制器中,唯一需要做的就是回显结果。

public function add_tag() { 
      echo $this->Mblog->addTag(); 
     } 

然后在你的jQuery的result将是你从控制器呼应的东西。