user3400419
user3400419

Reputation: 29

error inserting an array in codeigniter

when I insert an array in the db, I get the error "unidentified index" in some of the variables even though they all have their corresponding values. Tried using print_r, and it returned an array with values, but when I try to insert the array, the values become null. I dont understand why and I can't see the error in my code.

here's my controller

public function test()
{

    if($this->session->userdata('logged_in'))
    {
        $session_data = $this->session->userdata('logged_in');
        $user_id = $session_data['user_id'];

        $table_id = $this->input->post('table');
        $start = $this->input->post('start');
        $end = $this->input->post('end');

        $query = $this->process_resto_list->test($table_id);


        if ($query->num_rows() > 0)
        {
           $row = $query->row(); 

           $resto_id    = $row->resto_id;
           $table_number= $row->table_number;
           $table_size  = $row->table_size;
        }
        //$resto_id = $session_data['user_id'];

        $values = array(
                'resto_id'              => $resto_id,
                'diner_id'              => $user_id,
                'date'                  => $this->input->post('date'),
                'start_time'            => $start,
                'end_time'              => $end,
                'table_number'          => $table_number,
                'number_of_people'      => $table_size,
        );

        if($this->process_resto_list->insert_reservation($values))
            redirect('reservation_test/reservation', 'location');
        //print_r($values);
    }

Here's my model

public function insert_reservation($values)
{
    date_default_timezone_set("Asia/Manila");
        $data = array(

                'resto_id'              => $values['resto_id'],
                'diner_id'              => $values['user_id'],
                'date'                  => date('Y-m-d'),
                'start_time'            => $values['start'],
                'end_time'              => $values['end'],
                'table_number'          => $values['table_number'],
                'number_of_people'      => $values['table_size'],

            );

            return $this->db->insert('resto_reservations', $data);
}

When using print_r here's the result of the array

Array ( [resto_id] => 9 [diner_id] => 14 [date] => [start_time] => 11:00 [end_time] => 13:00 [table_number] => 6 [number_of_people] => 4 )

Please help me point out the error. Thank you

Upvotes: 0

Views: 49

Answers (2)

user3400419
user3400419

Reputation: 29

I just replaced my model with this. Thanks everyone!

$data = array(

            'resto_id'              => $values['resto_id'],
            'diner_id'              => $values['diner_id'],
            'date'                  => date('Y-m-d'),
            'start_time'            => $values['start_time'],
            'end_time'              => $values['end_time'],
            'table_number'          => $values['table_number'],
            'number_of_people'      => $values['number_of_people'],

        );

Upvotes: 0

Amol Bharmoria
Amol Bharmoria

Reputation: 239

In your model. Change values as follows

$data = array(

            'resto_id'              => $values['resto_id'],
            'diner_id'              => $values['diner_id'],
            'date'                  => date('Y-m-d'),
            'start_time'            => $values['start_time'],
            'end_time'              => $values['end_time'],
            'table_number'          => $values['table_number'],
            'number_of_people'      => $values['number_of_people'],

        );

Upvotes: 1

Related Questions