代码点火器中出现数据库错误


Database error occurred in codeigniter

有人能帮我查询出了什么问题吗?当我执行它时,输出显示如下。

Error Number: 1064

您的中有错误

SQL语法;查看与MySQL服务器版本相对应的手册,了解在"?,?,??,?"附近使用的正确语法在第1行

INSERT INTO touristspot(TouristspotID, AccountID, CategoryID, Name, Description, street, city_area, city) Values(0, 111, ?, ?, ?, ?, ?, ?)

我的控制器:

   public function manager_spotdetails(){

    $data = array(
        'CategoryID' => $this->input->post('category'),
        'Name' => $this->input->post('spotname'),
        'Description' => $this->input->post('desc'),
        'street' => $this->input->post('street'),
        'city_area' => $this->input->post('city_area'),
        'city' => $this->input->post('city')
    );
    $this->load->model('managerm');
    $data['cat'] = $this->managerm->category();
    $this->managerm->createspots($data);
    $this->load->view('manager_spotdetails', $data);

}

public function createTouristspot(){
    $this->load->model('managerm');
    $data['cat'] = $this->managerm->category();
    $this->load->view('manager_spotdetails');
}

我的型号:

    public function createspots($data){

    $b = $_SESSION['accountid'];
    $this->db->trans_start();
    $spot_id= $this->db->insert_id();
    $sql3= "INSERT INTO touristspot(TouristspotID, AccountID, CategoryID, Name, Description, street, city_area, city) Values($spot_id, $b, ?, ?, ?, ?, ?, ?)";      
    $this->db->query($sql3, $data);
    $this->db->trans_complete();
    return $this->db->insert_id();
}
    public function category(){
    $this->db->select('CategoryID');
    $this->db->select('CategoryType');
    $this->db->from('category');
    $query= $this->db->get();
    return $query->result();
}
}

我的观点:

                <div class="form-group">
                         <span class="input-group-addon" id="basic-addon1">Category Type</span>
                        <select class="form-control" name="category">
                            <?php foreach($cat as $row) {?>
                            <option value="<?php echo $row->CategoryID?>"><?php echo $row->CategoryType?></option>
                <?php }?>
                        </select>

谢谢!:)

更改$sql3字符串,在VALUES:中插入单引号(')

$sql3= "INSERT INTO touristspot(`TouristspotID`, `AccountID`, `CategoryID`, `Name`, `Description`, `street`, `city_area`, `city`) VALUES('$spot_id', '$b', ?, ?, ?, ?, ?, ?)"; 

使用此方法插入数据

控制器

$data = array(
      "COLUMN_NAME"=>$this->input->post("account_fname"),
      "COLUMN_NAME"=>$this->input->post("account_lname")
);
$insert = $this->Model_name->insert("table_name", $data);

型号

public function insert($table,$data){
        $this->db->insert($table,$data);
        $id = $this->db->insert_id();
        return $id;
}

添加引号后,尝试将列Name读取为sql中的关键字

$sql3= "INSERT INTO touristspot(`TouristspotID`, `AccountID`, `CategoryID`, `Name`, `Description`, `street`, `city_area`, `city`) Values('$spot_id','$b', '', '', '','', '', '')"; 

控制器中的方法manager_spotdetails应为

public function manager_spotdetails(){
$data = array(
    'AccountID'=>$_SESSION['accountid'],
    'CategoryID' => $this->input->post('category'),
    'Name' => $this->input->post('spotname'),
    'Description' => $this->input->post('desc'),
    'street' => $this->input->post('street'),
    'city_area' => $this->input->post('city_area'),
    'city' => $this->input->post('city')
);
$this->load->model('managerm');
$data['cat'] = $this->managerm->category();
$this->managerm->createspots($data);
$this->load->view('manager_spotdetails', $data);

}

在模型中创建点的方法应该是

public function createspots($data){
$this->db->trans_start();
$this->db->insert('touristspot', $data);
$this->db->trans_complete();
return $this->db->insert_id();

}

希望一切顺利。确保数组索引名称与数据库表列名相同。