2013-07-03 74 views
0

我試着做下面的事情,並且令人驚訝的是它只填充第一個表(new_guest),而另一個只寫入零值(new_reservation)。我該如何正確配置?CodeIgniter:通過來自同一表單的兩個數組填充兩個表?

控制器:

function guest_checks_in() {  

    $data_guest = array (
     'guest_title' => $this->input->post('guest_title'), 
     'guest_name' => $this->input->post('guest_name'), 
     'guest_gender' => $this->input->post('guest_gender'), 
     'guest_phone' => $this->input->post('guest_phone'), 
     'guest_email' => $this->input->post('guest_email'), 
     'guest_address' => $this->input->post('guest_address'), 
     'guest_province' => $this->input->post('guest_province'), 
     'guest_country' => $this->input->post('guest_country'), 
     'guest_postal_code' => $this->input->post('guest_postal_code'), 
     'guest_dob' => $this->input->post('guest_dob'), 
     'guest_nic_pp_dl' => $this->input->post('guest_nic_pp_dl') 
    ); 

    $data_reservation = array (
     'room_type' => $this->input->post('room_type'), 
     'meal_type' => $this->input->post('meal_type'), 
     'bed_type' => $this->input->post('bed_type'), 
     'ext_beds' => $this->input->post('ext_beds'), 
     'number_of_guests' => $this->input->post('number_of_guests'), 
     'start_date' => $this->input->post('start_date'), 
     'end_date' => $this->input->post('end_date'), 
     'reservation_duration' => $this->input->post('reservation_duration'), 
     'room_number' => $this->input->post('room_number'), 
     'total' => $this->input->post('total') 
    ); 

$this->reservations_model->add_guests($data_guest); 
$this->reservations_model->add_reservations($data_reservation); 
$this->confirm_check_in(); 

} 

型號:

class Reservations_model extends CI_Model { 

function add_guests($data_guest) { 
    $this->db->insert('new_guest', $data_guest); 
    return; 
} 

function add_reservations($data_reservation) { 
    $this->db->insert('new_reservation', $data_reservation); 
    return; 
} 

} 

回答

1

,如果你使用的交易將是不錯的。如果兩個表中的數據都必須輸入。

function guest_checks_in() {  

$data_guest = array (
    'guest_title' => $this->input->post('guest_title'), 
    'guest_name' => $this->input->post('guest_name'), 
    'guest_gender' => $this->input->post('guest_gender'), 
    'guest_phone' => $this->input->post('guest_phone'), 
    'guest_email' => $this->input->post('guest_email'), 
    'guest_address' => $this->input->post('guest_address'), 
    'guest_province' => $this->input->post('guest_province'), 
    'guest_country' => $this->input->post('guest_country'), 
    'guest_postal_code' => $this->input->post('guest_postal_code'), 
    'guest_dob' => $this->input->post('guest_dob'), 
    'guest_nic_pp_dl' => $this->input->post('guest_nic_pp_dl') 
); 

$data_reservation = array (
    'room_type' => $this->input->post('room_type'), 
    'meal_type' => $this->input->post('meal_type'), 
    'bed_type' => $this->input->post('bed_type'), 
    'ext_beds' => $this->input->post('ext_beds'), 
    'number_of_guests' => $this->input->post('number_of_guests'), 
    'start_date' => $this->input->post('start_date'), 
    'end_date' => $this->input->post('end_date'), 
    'reservation_duration' => $this->input->post('reservation_duration'), 
    'room_number' => $this->input->post('room_number'), 
    'total' => $this->input->post('total') 
); 

$這 - > reservations_model-> add_all_guests($ data_guest,$ data_reservation); $ this-> confirm_check_in();

}

模型

function add_all_guests($data_guest,$data_reservation) { 
$this->db->trans_begin(); 
$this->db->insert('new_guest', $data_guest); 
$this->db->insert('new_reservation', $data_reservation); 
if ($this->db->trans_status() === FALSE) 
{ 
    $this->db->trans_rollback(); 
    return false; 
} 
else 
{ 
$this->db->trans_commit(); 
return true; 
} 
} 
+0

我跟着你的腳步,目前正在填充兩個表。但對「交易」仍然沒有信心。必須閱讀CodeIgniter用戶指南。謝謝。 –

+0

當您想要在兩個表中添加數據並且必須輸入兩個數據時,事務非常有用。但也請閱讀用戶指南。 –

相關問題