Following is the portion in my view file.
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td><input type="text" name="description[]" placeholder="Enter your Description" class="form-control name_list" required="" /></td>
<td><input type="text" name="price[]" placeholder="Enter your Price" class="form-control name_list" required="" /></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
</table>
</div>
and this is my model
$query_status = $this->db->insert('events', $register_data_array);
$event_insert_id=$this->db->insert_id();
//insert the event ticket prices
$price_description = $_POST['description'];
$price = $_POST['price'];
if ( ! empty($price_description) && ! empty($price) )
{
foreach ($price_description as $key => $value) {
$data['event_id'] = $event_insert_id;
$data['price_description'] = $value;
$data['price'] = $price[$key];
$this->db->insert('event_ticket_prices',$data[]);
}
$this->db->trans_complete();
return true;
}
The insert_id is the last insert ID for the static information on the form. these gets saved without an issue, but for the dynamic tale I get an error.
Answer Your insert is in the wrong place. Move
$query_status = $this->db->insert('events', $register_data_array);
$event_insert_id=$this->db->insert_id();
after the foreach like this
foreach ($price_description as $key => $value) {
$query_status = $this->db->insert('events', $register_data_array);
$event_insert_id=$this->db->insert_id();
$data['event_id'] = $event_insert_id;
...
And your other insert is wrong
$this->db->insert('event_ticket_prices',$data);
And that will fix your issues.
Note : You should always empty your $data
array with each loop. Assuming your $data
array only has those 3 fields. Your trans->complete
is also in an odd spot as if it fails, does your code still do a trans->complete
?