简体   繁体   中英

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. The id field is AUTO_INCREMENT so I don't have to post it on array. But the problem is on the filename field which is a combination string of id and name with '-' delimiter. My expected results of filename is

+---+-------+-----------+
|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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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