Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm creating a bulk booking module. For that I need to look for available rooms, and assign rooms to that reservation. Each room will be added to reservation table individually via a loop. However this error message came instead.

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: mysql/mysql_driver.php
Line Number: 552

Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `reservations` (`guest_id`, `room_type`, `meal_type`, `extra_beds`, `purchases`, `guest_count`, `checkin`, `checkout`, `duration`, `total`, `room_number`, `guest_status`, `payment_status`, `travel_agent`) VALUES ('4', 'Single Room_10000', 'Breakfast_1000', '0_0', 0, '10', '2013-07-16', '2013-07-16', '1', '22250', Array, 'Arrived', 'Pending', '123456789V')

This is Controller:

function assign_available_rooms() {     
    $room_type = $this->input->post('room_type');
    $start_date = $this->input->post('start_date');
    $end_date = $this->input->post('end_date');
    $number_of_rooms = $this->input->post('number_of_rooms');
    $guest_nic_pp_dl = $this->input->post('guest_nic_pp_dl');
    $guest_id['guest_id'] = $this->group_reservations_model->retrieve_guest_id($guest_nic_pp_dl);
    $tmp_rooms['room_number'] = $this->group_reservations_model->get_room_numbers($room_type, $start_date, $end_date, $number_of_rooms);
    $newReservation = array (
        'guest_id' => $guest_id['guest_id'],
        'room_type' => $this->input->post('room_type'),
        'meal_type' => $this->input->post('meal_type'),
        'extra_beds' => $this->input->post('ext_beds'),
        'purchases' => 0,
        'guest_count' => $this->input->post('number_of_guests'),
        'checkin' => $this->input->post('start_date'),
        'checkout' => $this->input->post('end_date'),
        'duration' => $this->input->post('reservation_duration'),
        'total' => $this->input->post('total'),
        'room_number' => $tmp_rooms,
        'guest_status' => $this->input->post('guest_status'),
        'payment_status' => 'Pending',
        'travel_agent' => $this->input->post('guest_nic_pp_dl')
        );
    $this->group_reservations_model->assign_rooms($room_type, $start_date, $end_date, $number_of_rooms, $newReservation, $guest_id);
    $this->load->view('success');

}

This is my Model:

function assign_rooms($room_type, $start_date, $end_date, $number_of_rooms, $newReservation, $guest_id, $tmpRoomNumber = array()) {

    $query = $this->db->query(
        "SELECT a.room_number
        FROM rooms a LEFT OUTER JOIN (  SELECT room_number
                                        FROM reservations
                                        WHERE checkin >= '$start_date'
                                        OR checkout <= '$end_date'
                                        ) b
        ON a.room_number = b.room_number
        WHERE b.room_number is NULL
        AND a.room_type = SUBSTRING_INDEX('$room_type', '_', '1')
        AND a.housekeeping_status = 'Clean'
        ORDER BY a.room_number ASC
        LIMIT $number_of_rooms ");

    if($query->num_rows()>0) {
        foreach($query->result_array() as $row) {
            $this->db->trans_begin();
            $this->db->insert('reservations', $newReservation, $guest_id);

            if ($this->db->trans_status() === FALSE) {
                $this->db->trans_rollback();
                return false;
            }
            else {
                $this->db->trans_commit();
            return true;
            }
        }
    }       

}
share|improve this question
    
Before asking this question here, have you given it a shot on solving this yourself? The error is pretty easy to understand, you even have line numbers. –  N.B. Jul 16 '13 at 11:38
    
I'll give you a hint: "Unknown column 'Array' in 'field list'" –  jmadsen Jul 16 '13 at 11:39
add comment

1 Answer

up vote 1 down vote accepted

The error, like is says is occurring because you are trying to use an array as a string.

Change

'room_number' => $tmp_rooms

to

'room_number' => $tmp_rooms['room_number']

It's a good idea to have a go at debugging things like this yourself because from solving it yourself you'll learn how to do it next time you come across a similar error

share|improve this answer
    
Thank you. This solved the issue. But only single record is being updated. Must be an issue in "foreach" in the Model. –  Ivy Jul 16 '13 at 11:55
    
And this comment above mine is a perfect example of what happens when you give a solution to someone who uses 0% of their intellectual capacity to solve a trivial problem. Things learnt = 0. New problem? No problem! SO exists and there are people to do the thinking for me, I won't even bother to google. –  N.B. Jul 16 '13 at 12:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.