我的数据保存在codeigniter中的一列中

时间:2018-05-31 14:44:28

标签: codeigniter codeigniter-3

我尝试保存数据,但我data2保存column1时出错。我已将数据添加为数组

$simpan array(
 'column1' = $data1,
 'column2' = $data2,
 'column3' = $data3
);

但不想保存到我的数据库。

public function simpannilaidetail(){
    $id_nilai = $this->input->post('id_nilai');
    $id_pelajaran = $this->input->post('id_pelajaran');
    $get = $this->modelpenilaian->datanilai($id_nilai, $id_pelajaran);
    if (count($get) > 0) {
        $rs = "";
    }else{
        $nilaiawal = '0';

        **$rs = $this->crudmodel->simpannilaifix($id_nilai, $id_pelajaran, $nilaiawal);**   
    }

    echo $rs;
} 

public function simpannilaifix($idn,$idp,$nilaiawal){
    $this->db->query("INSERT INTO nilaidetail values('$idn','$idp','$nilaiawal')");
}

已保存,但结果是:

column1      column2    column3
data1,data2     NULL       data3

正确的结果必须是这样的:

column1 column2 column3
data1     data2    data3

感谢

1 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

使用像这样的普通查询

您还应提供列名

public function simpannilaifix($idn,$idp,$nilaiawal)
{
    /*change column_name with your real table column name*/

    $sql = "INSERT INTO nilaidetail (column_name1, column_name2, column_name3) 
            values('".$idn."','".$idp."','".$nilaiawal."')";
    $this->db->query($sql);
    echo $this->db->insert_id();
}

或者您可以使用ci insert query builder:

public function simpannilaifix($idn,$idp,$nilaiawal)
{
    /*change column_name with your real table column name*/

    $data = array('column_name1' => $idn,
                  'column_name2' =>$idp,
                  'column_name3' => $nilaiawal
            );
    $this->db->insert('nilaidetail',$data);
    return $this->db->insert_id();
}

更多信息:https://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data