我想存儲POST數據到數據庫和我已動態生成的場course[]
,start_date[]
等和我寫下面的代碼來檢索和存儲後的數據:後的數據不在數據庫中正確地存儲在笨
但執行後低於'0'的零都在所有字段中存儲。
if($this->input->post('course'))
{
$education_data= array();
for($i=0; $i<count($_POST['course']); $i++)
{
$education = array(
'course' => $this->input->post('course' . $i),
'start_date' => $this->input->post('start_date' . $i),
'end_date' => $this->input->post('end_date' . $i),
'college_name' => $this->input->post('college_name' . $i),
'other_info' => $this->input->post('other_info' . $i),
);
$this->db->insert('education',$education);
}
}
於是我嘗試了第二種方法:
if($this->input->post('course'))
{
$courses = $this->input->post("course");
$startdates = $this->input->post("start_date");
$end_date = $this->input->post("end_date");
$college_name = $this->input->post("college_name");
$other_infos = $this->input->post("other_info");
$education_data= array();
for($i=0; $i<count($_POST['course']); $i++)
{
$education = array(
'course' => $courses[$i],
'start_date' => $startdates[$i],
'end_date' => $end_date[$i],
'college_name' => $college_name[$i],
'other_info' => $other_infos[$i], //line number 101
);
// Insert Education data in 'education' table
$this->db->insert('education',$education);
}
}
但在這種情況下,只有一個迭代運行,並在第二次迭代它給在循環的第二次迭代的一些錯誤。
錯誤在第二次迭代:
Severity: Notice
Message: Undefined offset: 1
Filename: models/resume_model.php
Line Number: 101
A Database Error Occurred
Error Number: 1048
Column 'other_info' cannot be null
INSERT INTO `education` (`course`, `start_date`, `end_date`, `college_name`, `other_info`) VALUES ('', '', '', '', NULL)
Filename: C:\wamp\www\resume\system\database\DB_driver.php
Line Number: 330
如果有人知道請提出一個解決方案。
的var_dump輸出:
[course] => Array
(
[0] => course1
[1] => course2
)
[start_date] => Array
(
[0] => from1
[1] => from2
)
[end_date] => Array
(
[0] => to1
[1] => to2
)
[college_name] => Array
(
[0] => univ1
[1] => univ2
)
[other_info] => Array
(
[0] => other1
)
第二次迭代中給出的誤差是多少? – 2012-08-03 14:55:27
首先,不要使用'for'循環。使用'foreach'。其次,通過使用'var_dump()'調用來驗證變量的內容。 – Matt 2012-08-03 14:56:21
你可以給我們一個'var_dump($ _ POST)'可能是什麼樣子的例子嗎? – ghbarratt 2012-08-03 15:01:17