2016-05-05 68 views
0

有人可以幫助我什麼錯我的查詢?當我執行它時,輸出顯示如下。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> 

謝謝! :)在SQL

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

我無法保存我輸入到數據庫中的那一個 – Ruhinn

+0

在應用程序的config文件夾中轉到'database.php'並轉到'db_debug',並將其設置爲''db_debug'=> true,'Find實際的問題和解決它... –

回答

0

嘗試名稱讀作關鍵字更改$ SQL3字符串,VALUES插入單引號('):

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

我試過了,但它有相同的錯誤。 – Ruhinn

+0

@Ruhinn編輯在值變量 –

0

加上引號後列

+0

中添加了引號我必須在每個值中放一個單引號? – Ruhinn

+0

是的,你必須把每個值單引號 –

+0

我試過了,但它插入?在數據庫中。 – Ruhinn

0

使用這種插入數據的方法

控制器

$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; 
} 
+0

我明白了,謝謝。:) – Ruhinn

+0

如果您滿意,請接受我的回答。 –

0

在控制器的方法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); 

}

在模型的方法createspots應該是

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

}

希望它會正常工作。確保您的數組索引名稱與數據庫表列名稱相同。

+0

我會嘗試這一個。謝謝 :) – Ruhinn