数据未在codeigniter中插入数据库

时间:2016-09-21 07:21:09

标签: json codeigniter

实际上我在$ students数组中有学生记录,$ student中有另一个数组,其名称为skill [],这是一个复选框表单字段名称,所以请告诉我如何使用json_encode和where。

输入表格

<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>

控制器

 <?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";
                    }
            }
    }
    ?>

模型

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 Number: 1048

Column 'skills' cannot be null

INSERT INTO `student_info_table` (`name`, `email`, `skills`, `notes`, `gender`) VALUES ('gailyn', 'quentin@gmail.com', NULL, 'dsas', 'male')

Filename: C:/xampp/htdocs/codeigniter/system/database/DB_driver.php

Line Number: 691

2 个答案:

答案 0 :(得分:2)

据我了解,您希望从http请求获取技能数组,然后对其进行编码并将其保存到您的数据库中。为此请使用disable_multiple_selection而不是json_encode($this->input->post('skills'),因此您首先获取数据,然后对其应用json编码。

答案 1 :(得分:0)

试试这个:json_encode($this->input->post('skills'))您需要使用输入帖子获取技能的值,然后需要使用json_encode转换它

相关问题