我有一个包含一些输入字段和复选框的表单,我想在一列(行)中插入输入的所有值。
这是我的表格:
<form action="" method="post">
<label>Built in year</label>
<input name="feature[]">
<label>View</label>
<input name="feature[]">
这是我的控制器:
function create_listing(){
$this->load->view('form');
if($_POST){
$feature = array ( 'feature' => $_POST['feature'] );
foreach($feature as $fkey => $fvalue ){
$this->Mdata->f_detail($fvalue);
}
}
这是我的模特:
function f_detail($fvalue){
$this->db->insert('feature',$fvalue);
return $this->db->insert_id();
}
我收到错误:您的SQL语法有错误;查看与您的MariaDB服务器版本相对应的手册,以便在{0,1,2)VALUES附近使用正确的语法(&#39;在年份中构建&#39;,&#39;查看&#39;,& #39;停车位&#39;)&#39;在第1行
INSERT INTO feature
(0,1,2)VALUES(&#39;年份&#39;,&#39;查看&#39;,&#39;停车位&#39;)< / p>
我的代码中有什么问题。有人请告诉我。
此致
答案 0 :(得分:1)
在$this->input->post()
中使用$_POST()
代替codeigniter
两者都是等效的。
<强>控制器强>:
function create_listing(){
if($this->input->post()){
$feature = $this->input->post('feature');
foreach($feature as $fkey => $fvalue ){
$ids []= $this->Mdata->f_detail($fvalue);//$Ids is array of returned id
}
$this->load->view('form');//load view after database operations
}
<强>模型强>:
在您的模型中,您需要指定column name
,如下所示:
function f_detail($fvalue)
{
$this->db->insert('feature',array('column_name'=>$fvalue));//specify column name
return $this->db->insert_id();
}
答案 1 :(得分:1)
您可以使用implode
函数将多个值输入到一列中。
<强>控制器强>:
function create_listing(){
if($this->input->post()){
$data = array (
'feature' => implode(",", $this->input->post('feature'))
);
$this->Mdata->f_detail($data);
}
else{
$data = array ();
$this->load->view('form', $data);
}
}
<强>模型强>:
function f_detail($data){
$this->db->insert('feature',$data);
return $this->db->insert_id();
}