后的数据不在数据库中正确地存储在笨
我想存储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
)
更新的var_dump
从的var_dump你可以看到,other_info不会有'other_info' => $other_infos[1]
因为只有一个数组中的项目。如果您希望秒码正常工作,您可以向other_info添加另一个索引。
你错误
更新说明该值other_info为空过去了,从错误中我会假设你的表架构不支持空值for other_info列。您可以更改模式以允许空值,也可以在空值时将值传递给它。
请尝试以下操作并查看结果。我假设并非所有的数组都是相同的大小,这就是为什么你的第二次迭代失败。
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);
}
这只会隐藏潜在的问题(不张贴他期望他张贴的内容) – 2012-08-03 15:09:03
@ErwinMoller我不认为它是一个潜在的问题,我觉得other_info的价值不是必需的,当表单提交other_info为空,因此导致他正在看到的错误。它是一个确保值被设置的情况,并且如果它不传递除null以外的其他东西。 – 2012-08-03 15:14:19
@JoseVega在查看var_dump后感谢,我得到了脚本中的语法错误。谢谢 – Rajul 2012-08-03 15:23:36
$ _ POST [ '当然'] < - 包含数组,如你所说。
所以,如果你会print_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"
}
但在你的第一个代码,例如,你使用:
$这个 - >输入 - >后,
哪些没有按( '课程' $ I)。不存在(course12345不存在)。
在你的第二个代码示例,你这样做是正确的:
$courses = $this->input->post("course");
现在$课程持有数组,你似乎在你的不经意检查循环使用权(也许我错过了什么)。
我怀疑你最好的开始调试用一个简单的
echo "<pre>";
print_r($_POST);
echo "</pre>";
,并非常肯定您收到有望获得什么。 我怀疑你不会发布你认为你发布的所有内容。
确保您在所有使用的阵列中具有与您的课程阵列中一样多的元素。
如果没有看到$ _POST的实际内容,很难提供更多建议。
@Ervin我已经更新var_dump在上面的问题 – Rajul 2012-08-03 15:15:06
我明白了。 Jose Vega已经放大了你的问题,他是对的,所以我把你的问题留给他,然后继续前进。祝你好运! – 2012-08-03 15:20:09
第二次迭代中给出的误差是多少? – 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