我想從數組中插入幾個項目到數據庫中foreach
。我收到一個錯誤 Message: Undefined offset
。有人能幫我弄清楚是什麼造成的?用'foreach`從數組插入數組到數據庫
錯誤:
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);
你有沒有試過你的代碼?它有用嗎?如果不是,那麼你的預期效果不好?你有沒有收到任何錯誤訊息? –
是的,我得到錯誤:'消息:未定義偏移量' –
請編輯您的帖子並添加您收到的**確切**錯誤消息。 **確切的**,因爲它會讓人們更容易找出問題的可能性。 –