Welcome Guest, Not a member yet? Register   Sign In
Inserting into multiple tables with arrays
#1

[eluser]Loquela[/eluser]
Hi there I have two tables that I need to INSERT and UPDATE with data from a form.
My problem here is dealing with Arrays, which I struggle to get my around.

THE TABLES:

Code:
Table Topics:
topic_id
topic_name

Table topic2subject:
id
topic_id
subject_id

THE FORM VIEW (form_view.php):

Code:
<form action="submit_form.php">

<select multiple name="subject_id">
<option>Subject 1</option>
<option>Subject 2</option>
<option>Subject 3</option>
<option>Subject 4</option>
<option>Subject 5</option>
<option>Subject 6</option>
</select>

&lt;input type="hidden" name="topic" value="$topic" /&gt;
&lt;input type="Submit" name="submit" value="Submit form"
&lt;/form>

CONTROLLER (submit_form.php):

Code:
function submit_form()
{
   $data = array(
   'topic_id'        =>$this->input->post('topic_id'),
   'topic_name       =>$this->input->post('topic_name')
   'subjects'        =>$this->input->post('subject_id'),
);

   $this->library_model->process_form_data($data);
}

Code:
THE MODEL:
***************
???????????????
***************

I want the model function to insert the form data into 2 tables above. The topics table gets inserted/updated with the topic_id and topic_name (not subject_id). The topic2subject table should have a new row inserted for each selected option in the form and get the topic_id and subject_id.

Any help with this greatly appreciated.

Thanks in advance.

L.
#2

[eluser]sihijau[/eluser]
use insert table in foreach of course

take a look this article
http://codeigniter.com/wiki/Inserting_Mu...o_a_Table/
#3

[eluser]Krzemo[/eluser]
1. Start new transaction
2. Insert data into 1st table
3. Get last insert id
4. Insert data into 2nd table using last insert id from point above
5. Close transaction
6. Return transaction status or anything you might need from this model function

Voila Smile
#4

[eluser]WanWizard[/eluser]
Don't forget to use a database that is capable of using transactons, otherwise this does nothing.




Theme © iAndrew 2016 - Forum software by © MyBB