[英]Database error occurred in codeigniter
Can someone help me whats wrong with my query? 有人可以帮我查询问题吗? when I execute it the output shows like this. 当我执行它时,输出显示如下。
Error Number: 1064
You have an error in your 你有一个错误
SQL syntax; SQL语法; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?, ?, ?, ?)' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'?,?,?,?,?,?,?)'附近使用正确的语法
INSERT INTO touristspot(TouristspotID, AccountID, CategoryID, Name, Description, street, city_area, city) Values(0, 111, ?, ?, ?, ?, ?, ?)
my controller: 我的控制器:
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');
}
my Model: 我的模特:
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();
}
}
my Views: 我的看法:
<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>
Thank you! 谢谢! :) :)
在添加引号后尝试尝试在SQL中将名称列读取为关键字
$sql3= "INSERT INTO touristspot(`TouristspotID`, `AccountID`, `CategoryID`, `Name`, `Description`, `street`, `city_area`, `city`) Values('$spot_id','$b', '', '', '','', '', '')";
更改$ sql3字符串,在VALUES中插入单引号('):
$sql3= "INSERT INTO touristspot(`TouristspotID`, `AccountID`, `CategoryID`, `Name`, `Description`, `street`, `city_area`, `city`) VALUES('$spot_id', '$b', ?, ?, ?, ?, ?, ?)";
use this methods for insertion data 使用此方法插入数据
controller 控制者
$data = array(
"COLUMN_NAME"=>$this->input->post("account_fname"),
"COLUMN_NAME"=>$this->input->post("account_lname")
);
$insert = $this->Model_name->insert("table_name", $data);
Model 模型
public function insert($table,$data){
$this->db->insert($table,$data);
$id = $this->db->insert_id();
return $id;
}
The Method manager_spotdetails in Controller Should Be 控制器中的方法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);
} }
The Method createspots in Model Should Be 模型中的方法createpots应为
public function createspots($data){
$this->db->trans_start();
$this->db->insert('touristspot', $data);
$this->db->trans_complete();
return $this->db->insert_id();
} }
Hope It Will Works Fine. 希望它能正常工作。 Make sure your array index name same as database table column name. 确保您的数组索引名称与数据库表列名称相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.