简体   繁体   English

将AUTO_INCREMENT值分配给另一个字段

[英]Assign the AUTO_INCREMENT value to another field

Need your help. 需要你的帮助。 I have a table with 3 columns (id, name, filename) name and filename are a string. 我有一个包含3列(ID,名称,文件名)的表, namefilename是一个字符串。 The id field is AUTO_INCREMENT so I don't have to post it on array. id字段是AUTO_INCREMENT所以我不必将其发布在数组上。 But the problem is on the filename field which is a combination string of id and name with '-' delimiter. 但是问题出在filename段上,该字段是idname的组合字符串,并带有'-'分隔符。 My expected results of filename is 我对filename预期结果是

+---+-------+-----------+
|id | Name  | Filename  |
+---+-------+-----------+
| 1 | James | 1-James   |
| 2 | John  | 2-John    |
| 3 | Clark | 3-Clark   |
+---+-------+-----------+

My code is: 我的代码是:

    $data = array(  
        'name' = $this->input->post('name');
        'filename' =$this->input->id->post('name');
    )

    $this->db->insert('dbname', $data)
?>

You need to first insert data. 您需要首先插入数据。 Then get last inserted Id and using this you need to update record 然后获取最后插入的ID,并使用此ID更新记录

$data = array(  
        'name' => $this->input->post('name');
        'filename' => $this->input->id->post('name');
    )

    $this->db->insert('dbname', $data);
    $last_id = $this->db->insert_id();
    $new_file_name = $last_id."-".$this->input->post('name');
    $update_data = array(
        'filename' => $new_file_name;
      );
    $this->db->where('id',$last_id);
    $this->db->update('dbname', $update_data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM