CodeIgniter Forums
[Resolved] 2 tables in one form - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [Resolved] 2 tables in one form (/thread-3782.html)



[Resolved] 2 tables in one form - El Forum - 10-22-2007

[eluser]Yash[/eluser]
I want to update 2 tables using a single form.How can I do this?

like I want to distribute information in 2 tables using single form.


[Resolved] 2 tables in one form - El Forum - 10-22-2007

[eluser]xwero[/eluser]
Forms aren't coupled with database tables. Forms just gather information from the user and you have to program how that information gets stored.
view file
Code:
<form>
<input type="text" name="field1">
<input type="text" name="field2">
<input type="submit" value="send">
</form>
controller
Code:
if(isset($_POST))
{
    // validation
    $this->load->model('Somemodel','',true);
    $this->Somemodel->insert($_POST);
}
model
Code:
function insert($post)
{
    $this->db->query('insert into table1 (field1) values (?)',array($post['field1']));
    $this->db->query('insert into table2 (field1) values (?)',array($post['field2']));
}

This is example code so don't use this for production sites. I hope this gets you on your way.


[Resolved] 2 tables in one form - El Forum - 10-22-2007

[eluser]Yash[/eluser]
u rocks...

Is it necessary to use model?.What is that some model.I'm new to this CI.

http://aquireknowledge.com/phpframework/index.php/control/insertstory

Here I want to insert top 2 fields in one table and other in second.I'm using CI validation class.

Thank you for fast reply.


[Resolved] 2 tables in one form - El Forum - 10-22-2007

[eluser]xwero[/eluser]
Models are where you store all the functions that have to do with database manipulation. It is a part of the MVC pattern which is used by CI to create easier to maintain sites. CI does other things too but this is the heart of the framework.

The creation of models isn't much different that creating a controller you just extend the Model class instead of the Controller class.


[Resolved] 2 tables in one form - El Forum - 10-22-2007

[eluser]Yash[/eluser]
now I got it..Thank you

I like model approach.It help me creating what I want.


U rocks man