0

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

8
  • Please give us the exact text of the error message Commented Jan 19, 2015 at 17:24
  • A PHP Error was encountered Severity: Notice Message: Undefined index: user_id Filename: models/process_resto_list.php Line Number: 72 @Bulk Commented Jan 19, 2015 at 17:25
  • You're assigning user_id to diner_id in the controller. Commented Jan 19, 2015 at 17:26
  • Undefined index also in start, end and table_size Commented Jan 19, 2015 at 17:26
  • and this error Error Number: 1048 Column 'diner_id' cannot be null INSERT INTO resto_reservations (resto_id, diner_id, date, start_time, end_time, table_number, number_of_people) VALUES ('9', NULL, '2015-01-20', NULL, NULL, '6', NULL) Filename: C:\xampp\htdocs\forktal\system\database\DB_driver.php Line Number: 330 @Bulk Commented Jan 19, 2015 at 17:26

2 Answers 2

1

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'],

        );
Sign up to request clarification or add additional context in comments.

2 Comments

Right!!! so stupid of me! I am literally angry at myself right now for not noticing that error. Thank you so much @AmBhar
@user3400419 Glad to know your problem is solved. :)
0

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'],

        );

Comments

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.