I want to insert several items from an array into a database with foreach
. I get an error
Message: Undefined offset
. Can someone help me figure out what's causing it?
error:
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Line Number: 127
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Line Number: 128
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Line Number: 129
php:
<?php
$guide_input = $this->input->post('guide');
$airline_input = $this->input->post('airline');
$name_r_input = $this->input->post('name_r');
$units_input = $this->input->post('units');
$price_change_input = $this->input->post('price_change');
$guide = array();
$airline = array();
$date_go = array();
$date_back = array();
$residence = array();
foreach ($guide_input as $idx => $name) {
$guide[] = array(
'name_guide' => $guide_input[$idx], //Line Number: 121
);
$airline[] = array(
'name_airline'开发者_如何学编程 => $airline_input[$idx], //Line Number: 124
);
$residence[] = array(
'name_r' => $name_r_input[$idx], //Line Number: 127
'units' => $units_input[$idx], //Line Number: 128
'price_change' => $price_change_input[$idx], //Line Number: 128
);
};
$data = array(
'json1' => json_encode($residence),
'json2' => json_encode($airline),
'json3' => json_encode($guide),
);
$this->db->insert(tableName, $data);
above code assumes that all input arrays like $airline_input, $date_go_input and so on.. will have the same number of elements as $guide_input in them. My guess is they dont.
For the code to be correct and run without errors yu should check:
count($guide_input) == count($airline_input)
&& count($guide_input) == count($date_go_input)
// ... and so on... //
I think you should check guide_input
, airline_input
, and so on. Every arrays has same keys?
精彩评论