Reputation: 247
I have a form which contains some input fields and checkboxes, I want to insert all the values of input in one column (rows).
Here is my form:
<form action="" method="post">
<label>Built in year</label>
<input name="feature[]">
<label>View</label>
<input name="feature[]">
here is my controller:
function create_listing(){
$this->load->view('form');
if($_POST){
$feature = array ( 'feature' => $_POST['feature'] );
foreach($feature as $fkey => $fvalue ){
$this->Mdata->f_detail($fvalue);
}
}
and here is my model:
function f_detail($fvalue){
$this->db->insert('feature',$fvalue);
return $this->db->insert_id();
}
I am getting an error :
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 '0, 1, 2) VALUES ('build in year', 'view', 'parking space')' at line 1
INSERT INTO `feature` (0, 1, 2) VALUES ('build in year', 'view', 'parking space')
What's wrong in my code. Anyone please tell me .
Regards
Upvotes: 0
Views: 8986
Reputation: 473
You can input multiple value into one column with implode
function.
Controller:
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);
}
}
Model:
function f_detail($data){
$this->db->insert('feature',$data);
return $this->db->insert_id();
}
Upvotes: 2
Reputation: 6994
Use $this->input->post()
instead of $_POST()
in codeigniter
both are equivalent.
Controller:
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
}
Model:
In your model you need to specify column name
like below:
function f_detail($fvalue)
{
$this->db->insert('feature',array('column_name'=>$fvalue));//specify column name
return $this->db->insert_id();
}
Upvotes: 2