插入数据库codeigniter时出现sql语法错误

时间:2019-07-17 10:07:43

标签: php mysql sql codeigniter

我遇到一个错误,提示“您的SQL语法有错误;请查看与您的MariaDB服务器版本相对应的手册以使用正确的语法” 我对Codeigniter还是很陌生,我真的需要帮助 目前,这是我一直在尝试应用的代码

function save() {

        $reporter=$this->Settings_m->greporter();
        $jabrik=$this->Settings_m->gjabrik();
        $redpel=$this->Settings_m->gredpel();

        $usertxt="";
        if (in_array($this->session->userdata('ugroup'), $reporter )) {
            $usertxt=",reporter=".$this->user.",reporter_time=now()";
        }
        if (in_array($this->session->userdata('ugroup'), $jabrik )) {
            $usertxt=",jabrik=".$this->user.",jabrik_time=now()";
        }
        if (in_array($this->session->userdata('ugroup'), $redpel )) {
            $usertxt=",redpel=".$this->user.",redpel_time=now()";
        }

        /* end for usertxt call */

        if($this->input->post('pub') == 1){
            if($this->input->post('tglpub') == "0000-00-00 00:00:00" || $this->input->post('tglpub') == ""){
                $tgl=",publish_up=now()";
            }else{
                //$tgl = $this->input->post('tglpub');
                $tgl = ",publish_up='".$this->input->post('tglpub')."'";
            }
        }else{
            $tgl=",publish_up=''";
        }

        $title =str_replace('"',"'",$this->input->post('title'));

        $query=" insert gis_news_items set "
              .' title="' .$title.'"'
              .",alias='" . $this->input->post('alias',true )."'"
              .",introtext='". $this->input->post('content')."'"
              .",fulltexts=' '"
              .",catid=".$this->input->post('catid')
              .",published=".$this->input->post('pub')
              // .",hl=".$this->input->post('hl')
              // .",focus=".$this->input->post('fcs')
              // .",byline=".$byline
              .$usertxt 
              .",modified_by=" . $this->user 
              // .",publish_up='" .$this->input->post('tglpub')."'"
              .$tgl
              .',metadesc="' . str_replace('"',"'", $this->input->post('deskripsi',true)  )   .'"'
              .',metakey="' . str_replace('"',"'", $this->input->post('keyword',true)  )      .'"' ;
        $this->M_general->add_data("gis_news_items", $query);      

    }

编辑
这是我从查询中获得的数据,我尝试使用此处的建议作为参考来对其进行调整,但仍然出现错误,因此我撤消它以查看错误

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'insert into gis_news_items values ( title="sepatu nike 5m",alias='sepatu-nike-5' at line 1

INSERT INTO `gis_news_items` ( insert into gis_news_items values ( title="sepatu nike 5m",alias='sepatu-nike-5m',introtext='

sepatu nike seharga 5m dengan desain indomie
',fulltexts=' ',catid=586,published=0,modified_by=1150,publish_up='',metadesc="sepatu desain indomie",metakey="sepatu, indomie") VALUES ('')

任何帮助和解释将不胜感激,因为我仍在学习如何编写良好的查询
谢谢:)

2 个答案:

答案 0 :(得分:0)

您的SQL查询无效。

$query=" insert gis_news_items set "

应该是

$query=" insert into gis_news_items values( "
....

在此处签出更多示例 https://mariadb.com/kb/en/library/insert/

答案 1 :(得分:0)

尝试sql

FIELDWIDTHS="3 3 3 3 3 3 3 3"