【问题标题】:How to get auto generated id after insertion? [duplicate]插入后如何获取自动生成的ID? [复制]
【发布时间】:2019-12-10 17:28:42
【问题描述】:

我用自动生成的 id 在数据库中插入记录,现在我想在点击提交按钮时获取该 id,这会给我带来多文件上传页面

【问题讨论】:

  • $this->db->insert_id() 是最后插入的

标签: php sql codeigniter


【解决方案1】:

试试这个

function add_post($post_data){
   $this->db->insert('posts', $post_data);
   $insert_id = $this->db->insert_id();

   return  $insert_id;
}

如果有多个插入,您可以使用

$this->db->trans_start();
$this->db->trans_complete();

【讨论】:

  • 使用$assignment_id=$this->db->insert_id(); 而不是$this->db->assignment_id();
  • 我想通过重定向在其他页面上传递该 ID,可以吗?
  • 好的,只要把这个‘id’当作参数插入就行了
  • 但如果您只从事 Web 开发工作,那么使用会话变量的方法会更好
  • 这样做是这样的:redirect('Welcome/file_upload/assignment_id='.$res); $res 包含自动生成的 id
【解决方案2】:
$conn->insert_id

会给你最后一个插入ID w3schools

【讨论】:

    【解决方案3】:
    $this->db->insert_id();
    

    这将在插入记录后为您提供最后插入的 id。

    【讨论】:

      猜你喜欢
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多