Reputation: 101
actually i am having student records in $students array and there is another array within $students, whose name is skill[], which is a checkbox form field name, so pls tell me how to use json_encode and where.
input form
<td>ENTER SKILLS</td>
<td>
<input type="checkbox" name="skills[]" value="php">php<br>
<input type="checkbox" name="skills[]" value="dotnet">dotnet<br>
<input type="checkbox" name="skills[]" value="java">java<br>
<input type="checkbox" name="skills[]" value="ruby_on_rails">ruby_on_rails<br>
</td>
controller
<?php
public function insert(){
if ($this->input->post('add')==true)
{
$student = array( 'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'skills' => $this->input->post(json_encode(skills)),
'notes' => $this->input->post('notes'),
'gender' => $this->input->post('gender') );
$result = $this->Student_info_model->insertStudent($student);
if($result==true){
echo "inserted";
}
else {
echo "Not Inserted";
}
}
}
?>
model
function insertStudent($student){
$this->db->insert('student_info_table', $student); // insert data into "student_info_table" table`
if ($this->db->affected_rows() > 0) {
return true;
}
else {
return false;
}
}
error
Error Number: 1048
Column 'skills' cannot be null
INSERT INTO `student_info_table` (`name`, `email`, `skills`, `notes`, `gender`) VALUES ('gailyn', '[email protected]', NULL, 'dsas', 'male')
Filename: C:/xampp/htdocs/codeigniter/system/database/DB_driver.php
Line Number: 691
Upvotes: 0
Views: 319
Reputation: 845
Try with this : json_encode($this->input->post('skills'))
You need to get the value of skills using input post and then need to convert it using json_encode
Upvotes: 0
Reputation: 1514
As far as i understand you want to get the array of skills from the http request and then to encode it and save it to your database. For that please use json_encode($this->input->post('skills')
instead of $this->input->post(json_encode(skills))
, so you first get the data, and then apply the json encoding over it.
Upvotes: 2