Harris Khan Harris Khan -4 years ago 145
MySQL Question

how to insert multiple fields data in one column in codeigniter

I have a form consist of multiple checkbox and input fields, I want to insert that data into a single column of a table, here is my form:

<div id="container">
<h1>property Detail</h1>
<form action="" method="post">
<table>
<tr>
<td>
Possesion
<input type="checkbox" name="feature[]" value="possesion">
</td>
<td>
Possesion1
<input type="checkbox" name="feature" value="possesion1">
</td>
<td>
Possesion2
<input type="checkbox" name="feature" value="possesion2">
</td>
<td>
Possesion3
<input type="checkbox" name="feature" value="possesion3">
</td>
<td>
Possesion4
<input type="checkbox" name="feature" value="possesion4">
</td>
</td>
</tr>

<tr>
<td> <input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>

</div>


here is my controller:

function index(){
$this->load->view('form');

if($_POST){


$data_feature = array (
'feature' => $_POST['feature']
);

$data['var']= $this->Mdata->p_detail($data_feature);
}

}


and here is my model:

function p_detail($data_feature){
$this->db->insert('feature',$data_feature);
return $this->db->insert_id();


}


I am getting only one feature value in my table, I want to get all the values of check boxes which user checked.

Regards

Answer Source

In view change all name="feature" to name="feature[]".

Best way is save values in column as json string.Like below.

Controller:

function index(){
            $this->load->view('form');

            if($_POST){
            $features = $_POST['feature'];//array of features
            $json = json_encode($features);//json string you need to save it in database
            $data['var']= $this->Mdata->p_detail($json);
            }   

        }

And

Model:

  function p_detail($data_feature){
             $data = array('feature'=>$data_feature);//sets json string value to feature column
             $this->db->insert('feature',$feature);//inserts into a single column 
             return $this->db->insert_id();//returns last inserted id

}

You need to use json_decode() for getting these values in array from database.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download