【问题标题】:how to get last inserted id after inserting using codeigniter and jquery使用codeigniter和jquery插入后如何获取最后插入的id
【发布时间】:2017-02-06 11:39:01
【问题描述】:

请帮助我了解如何使用 codeigniter 和 jquery 从数据库中获取最后插入的 id。我想要的是在插入后获取最后插入的 id。我正在使用 jquery 插入数据。我不知道该怎么做。提前非常感谢。

脚本

$('#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);
            }
        });
    }
});

查看

<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();
}

型号

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();
}

【问题讨论】:

  • 你有 mysql LAST_INSERT_ID() 函数
  • @Drew 您能否提供相同问题的链接?谢谢。
  • 你也可以这样做 mysql_query("INSERT INTO testtable(1, 2, 3, 'some')"); $last_insert_id = mysql_insert_id();
  • $this->db->insert('posts', $post_data); $insert_id = $this->db->insert_id();返回 $insert_id;
  • @vamsi 我知道该怎么做。但我想要的是用 jquery 返回它。

标签: php jquery mysql ajax codeigniter


【解决方案1】:

将返回添加到您的控制器函数或回显结果,如下所示:

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

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

    exit;
}

然后尝试修改dump,看看ajax成功响应:

$.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
    }
});

试试这些,让我们知道这是否适合您。

【讨论】:

  • 很高兴为您提供帮助 :) 感谢您将其标记为正确答案。
【解决方案2】:

如果您问的是如何将数据从控制器返回到 jQuery,那么是的,这就是答案。

用这个替换你的控制器代码

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

回显的结果会出现在你成功的jQuery.ajax中。

【讨论】:

    【解决方案3】:

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

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

    然后在您的 jquery 中,result 将是您从控制器回显的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      相关资源
      最近更新 更多