我想将帖子数据存储到数据库中,并且我有动态生成的字段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
)