codeigniter无法将记录插入数据库

时间:2012-10-15 03:51:53

标签: php database codeigniter activerecord

我收到了这样的错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Satuan, `Satuan`) VALUES (‘KMJ001’, ‘KKMJ005’, ‘asdf asdf’, ‘123123’, ‘kg’)’ at line 1

INSERT INTO `ms_material_jasa` (`Kode_Material_Jasa`, `Kode_Kategori_Material_Jasa`, `Nama_Jenis_Material_Jasa`, `Harga` Satuan, `Satuan`) VALUES (‘KMJ001’, ‘KKMJ005’, ‘asdf asdf’, ‘123123’, ‘kg’)

它指的是我的活动记录模型

function create($kmj,$kkmj,$njmj,$hs,$s)
{
    $data=array('Kode_Material_Jasa' => $kmj, 'Kode_Kategori_Material_Jasa' => $kkmj, 'Nama_Jenis_Material_Jasa' => $njmj, 'Harga Satuan' => $hs, 'Satuan' => $s);
    $this->db->insert('ms_material_jasa',$data);

}  

这是我的控制器

 function tambah()
{
   $data['kmj'] = $this->input->post('kmj');
   $data['kkmj'] = $this->input->post('kkmj');
   $data['njmj'] = $this->input->post('njmj');
   $data['hs'] = $this->input->post('hs');
   $data['s'] = $this->input->post('s');


   $data['title'] = 'QB Tambah Material / Jasa';
   $data['pesan'] = $data['kmj'].' berhasil ditambahkan ! ';


    $this->form_validation->set_rules('kmj','Kode Material / Jasa','trim|required|min_length[2]|max_length[20]|xss_clean');
    $this->form_validation->set_rules('kkmj','Kode Kategori Material / Jasa','trim|required|min_length[2]|max_length[20]|xss_clean');
    $this->form_validation->set_rules('njmj','Nama Jenis Material / Jasa','trim|required|min_length[2]|max_length[20]|xss_clean');
    $this->form_validation->set_rules('hs','Harga Satuan','trim|required|min_length[2]|numeric|max_length[20]|xss_clean');
    $this->form_validation->set_rules('s','Satuan','xss_clean');

    if($this->form_validation->run() == false)
    {
        $lastval = $this->m_admin->getlastval('KMJ','ms_material_jasa','kode_material_jasa');
        $data['nextval'] = $this->m_admin->gencode('KMJ',3,$lastval); 


        $this->load->view('head',$data);
        $this->load->view('content/add_material_jasa',$data);
    }
    else
    {

        $this->m_materialjasa->create($data['kmj'],$data['kkmj'],$data['njmj'],$data['hs'],$data['s']);
        $data['kirim'] = $this->m_materialjasa->read();
        $this->load->view('head',$data);
        $this->load->view('v/vkategorimaterial',$data);


    }

}  

看起来像以下查询

INSERT INTO `ms_material_jasa` (`Kode_Material_Jasa`, `Kode_Kategori_Material_Jasa`, `Nama_Jenis_Material_Jasa`, `Harga` Satuan, `Satuan`) VALUES (‘KMJ001’, ‘KKMJ005’, ‘asdf asdf’, ‘123123’, ‘kg’)

是正确的,这是ms_material_jasa表。看起来这个条目不会破坏数据库中的数据类型。我不知道出了什么问题,我该如何解决? enter image description here

5 个答案:

答案 0 :(得分:0)

你的引语在字段列表中的'Harga Satuan'中的位置错误。错误实际上是这样说的。 你的:

INSERT INTO `ms_material_jasa` (`Kode_Material_Jasa`, `Kode_Kategori_Material_Jasa`, `Nama_Jenis_Material_Jasa`, `Harga` Satuan, `Satuan`) VALUES (‘KMJ001’, ‘KKMJ005’, ‘asdf asdf’, ‘123123’, ‘kg’)

正确:

INSERT INTO `ms_material_jasa` (`Kode_Material_Jasa`, `Kode_Kategori_Material_Jasa`, `Nama_Jenis_Material_Jasa`, `Harga_Satuan`, `Satuan`) VALUES (‘KMJ001’, ‘KKMJ005’, ‘asdf asdf’, ‘123123’, ‘kg’)

答案 1 :(得分:0)

您输入的字段名称不正确。请更正此问题,将Harga_Satuan代替Harga Satuan

function create($kmj,$kkmj,$njmj,$hs,$s)
{
    $data=array('Kode_Material_Jasa' => $kmj, 'Kode_Kategori_Material_Jasa' => $kkmj, 'Nama_Jenis_Material_Jasa' => $njmj, 'Harga_Satuan' => $hs, 'Satuan' => $s);
    $this->db->insert('ms_material_jasa',$data);

}  

,您的查询应为

INSERT INTO `ms_material_jasa` (`Kode_Material_Jasa`, `Kode_Kategori_Material_Jasa`, `Nama_Jenis_Material_Jasa`, `Harga_Satuan`, `Satuan`) VALUES (‘KMJ001’, ‘KKMJ005’, ‘asdf asdf’, ‘123123’, ‘kg’)

答案 2 :(得分:0)

问题可能是听到了 'Harga Satuan'=> $ HS 没有这样的领域。

答案 3 :(得分:0)

正确的是

INSERT INTO `ms_material_jasa` (`Kode_Material_Jasa`, `Kode_Kategori_Material_Jasa`, `Nama_Jenis_Material_Jasa`, `Harga Satuan`, `Satuan`) VALUES ('KMJ001', 'KKMJ005', 'asdf asdf', '123123', 'kg')

答案 4 :(得分:0)

问题是活动记录未能将撇号放入Harga Satuan,

 $data=array('Kode_Material_Jasa' => $kmj, 'Kode_Kategori_Material_Jasa' => $kkmj, 'Nama_Jenis_Material_Jasa' => $njmj, 'Harga Satuan' => $hs, 'Satuan' => $s);
    $this->db->insert('ms_material_jasa',$data);

可能是因为单词之间和数据库之间的空格是和下划线。 纠正这个改变这样的代码。

function create($kmj,$kkmj,$njmj,$hs,$s)
{
  $data=array('Kode_Material_Jasa' => $kmj, 'Kode_Kategori_Material_Jasa' => $kkmj, 'Nama_Jenis_Material_Jasa' => $njmj, 'Harga_Satuan' => $hs, 'Satuan' => $s);
$this->db->insert('ms_material_jasa',$data);


}  

希望它能解决问题。