Search code examples
databasecodeigniterdynamicinsert

Codeigniter dynamic insert


I am new to CodeIgniter. I've already created the required program, but I want to make it smaller and more effective. I have a database with 10+ tables, where I just insert/update into almost the same rows (+/- 2 rows).

I would like to make a controller which inserts all the incoming data to the url specified table. Like: insert/news -> inserting data into news table, and all the [input] name's are equal to the table's column name.

So I just submit my form at url: insert/news where I have [input name="content"] and it automatically inserts into news table and content row.

Is it a good approach, or I should make separately 10+ models with create functions?

Thanks, and sorry for my newbie question!


Solution

  • There is no problem with that approach, and it's good if it fits your needs.

    Use

     $this->uri->uri_to_assoc(x);
    

    to get the table name. Validate the data:

    $this->input->post(...);
    

    And for even more effective app make those controllers calls as AJAX requests.