我想将post数据存储到数据库中,我已经动态生成了字段course[]
、start_date[]
等,我编写了以下代码来检索和存储post数据:
但在执行以下操作后,所有字段中仅存储"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
)
更新var_dump
从var_dump中,您可以看到other_info不会有'other_info' => $other_infos[1]
,因为数组中只有一个项。如果您希望秒代码正常工作,您可以向other_info添加另一个索引。
@JoseVega的更新我更新了上面的问题,但有错误
您的错误是声明为other_info传递的值为null,因此我认为您的表架构不支持other_info列的null值。您可以更改模式以允许null值,也可以在其为null时向其传递值。
尝试以下操作,然后查看结果。我假设不是所有的数组都是相同大小的,这就是为什么第二次迭代失败的原因。
for($i=0; $i<count($courses); $i++) {
$education = array(
'course' => isset($courses[$i]) ? $courses[$i] : "",
'start_date' => isset($startdates[$i]) ? $startdates[$i] : "",
'end_date' => isset($end_date[$i]) ? $end_date[$i] : "",
'college_name' => isset($college_name[$i]) ? $college_name[$i] : "",
'other_info' => isset($other_infos[$i]) ? $other_infos[$i] : "",
);
// Insert Education data in 'education' table
$this->db->insert('education',$education);
}
$_POST['course']<--包含一个数组,正如您所说。
因此,如果您要打印_r($POST);你会看到这样的东西:
array(2) {
["course"]=>
array(3) {
[0]=>
string(5) "17740"
[1]=>
string(5) "18422"
[2]=>
string(5) "14170"
}
["startdate"]=>
array(3) {
[0]=>
string(10) "2012-01-02"
[1]=>
string(10) "2012-01-02"
[2]=>
string(10) "2012-01-02"
}
但在您的第一个代码示例中,您使用:
$this->input->post('course'.$i),
它不存在(课程12345不存在)。
在您的第二个代码示例中,您做对了:
$courses = $this->input->post("course");
现在$courses持有数组,您在随意检查时似乎正好在循环中使用该数组(也许我遗漏了什么)。
我怀疑你最好从一个简单的开始调试
echo "<pre>";
print_r($_POST);
echo "</pre>";
并确保你收到了你期望收到的东西。我怀疑你没有发布你认为发布的所有内容。
确保所有使用的数组中的元素数量与课程数组中的相同。
如果没有看到$_POST的实际内容,很难提供更多建议。