I want to store post data into database and i have dynamically generated fields course[]
, start_date[]
etc and i write following code to retrieve and store post data:
but after execution of below only '0' zeros are storing in all fields.
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);
}
}
so then i tried second method:
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);
}
}
But in this case only one iteration is running and at second iteration it is giving some error in second iteration of loop.
error in second iteration:
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\wwwesume\system\database\DB_driver.php
Line Number: 330
If anybody know please suggest a solution.
var_dump output:
[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
)
Update for var_dump
From the var_dump you can see that other_info will not have a 'other_info' => $other_infos[1]
because there's only one item in the array. You can add another index to other_info if you would like for your seconds code to work correctly.
Update for @JoseVega I have updated above question with error
You error is stating that the value passed for other_info is null, from the error I would assume that your table schema does not support a null value for other_info column. You can either change your schema to allow null values or you can pass it a value when its null.
Try the following, and see your results. I am assuming that not all arrays are of the same size and thats why your second iteration fails.
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'] <-- contains an array, as you stated.
So if you would print_r($POST); you would see something like:
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"
}
But in your FIRST code-example you use:
$this->input->post('course' . $i),
Which doesn't exist (course12345 doesn't exist).
In your second code-example, you do it right:
$courses = $this->input->post("course");
Now $courses holds the array, which you seem to use right in your loop on casual inspection (maybe I miss something).
I suspect you best start debugging with a simple
echo "<pre>";
print_r($_POST);
echo "</pre>";
and make very sure you receive what you expected to receive. I suspect you don't post everything you think you are posting.
Make sure you have as many elements in all used arrays as you have in your courses array.
It is hard to advice more without seeing the actual content of $_POST.