dongtang1909 2019-01-14 16:57
浏览 22


I am trying to insert the names (First name and Last name) details of a user registering into [first name] and [last name] column of a user_details table. Consequently, the email and password to their respective columns of a snm_users table

How can I possibly achieve this in Codeigniter?

Here is snippet of my input form:

<input type="text" name="fname">
<input type="email" name="email">
<input type="password" name="password">

And here is the model to handle database:

class User_model extends CI_Model {
    public function register ($enc_password){
        $data = array(
            'fname' => $this->input->post('fname'),
            'email' => $this->input->post('email'),
            'password' => $enc_password
        //insert user to database
        return $this->db->insert('snm_users', $data);
  • 写回答

1条回答 默认 最新

  • douban2014 2019-01-15 08:41

    I believe you are new to CodeIgniter. Better do some research to get a basic idea of how the MVC (Model View Controller) really works. I will give you a basic idea: VIEW is where you use the HTML, like displaying the form, and any output stuff, etc. MODEL is where you write the code for db interactions. Like INSERTIONS, UPDATIONS, SELECT queries, etc. And CONTROLLER is where you do you business logic, validations, etc.

    In the code snippet you posted above, you are using the data submitted from the form directly inside your MODEL. Which is kind of not the best approach. Accept it (from $this->input->post() inside your CONTROLLER, and then do the validations and pass it to the MODEL.

    And back to your question, you want to insert the first_name and last_name in user_details_table and email and password in another table (snm_users_table)

    For this, first do the insertion in your main table. I believe its snm_users_table. And I assume that you have a primarykey named user_id or something to identify each record. So, once you insert in this main User table, you can get the last_insert_id(id of this newly inserted row) using this function: $this->db->insert_id()

    Now, you can use this user_id returned by the above function to insert the other details on your second table, which is user_details_table.

    You can use TRANSACTIONS during these insertions, so that you won't end up inserting a record in one table and no record in the other table, in case of any errors! Because TRANSACTIONS would help you to rollback the changes in case of any problems!

    Read more here:

    Hope this will give you an idea. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误