0

I tried doing the following and surprisingly it only populates the first table (new_guest) while writing only zero values to the other (new_reservation). How can I properly configure this?

Controller:

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();

}

Model:

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 Answer 1

1

it will be good if you use transaction. if data in both tables must be entered.

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_all_guests($data_guest,$data_reservation); $this->confirm_check_in();

}

in model

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;
 }
}
2
  • I followed your steps and now both tables are being populated. But still not confident about "transaction". Must read CodeIgniter user guide. Thank you. Commented Jul 3, 2013 at 17:12
  • transaction is very useful when you want to add data in two tables and both data must be entered. But read the user guide too. Commented Jul 3, 2013 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.