how to insert multiple fields data in one column in codeigniter

∥☆過路亽.° 提交于 2019-12-25 06:10:06

问题


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


回答1:


Best way to save it is json_encode to make the data in json string.There are many methods to save data like this:

Firstly correct the html to make all same name fields as array like:

<input type="checkbox" name="feature[]" value="possesion">

Method 1:

Make your data as json string to save:

$this->db->insert('feature',json_encode($data));//data is an array need to insert()

Method 2:

Make your data as serialize array:

$this->db->insert('feature',serialize($data));

Method 3:

Make your data array as string:

$this->db->insert('feature',implode(",",$data));

And if you want to row by row insertion then iterate posted values over loop like below:

function index(){

            $this->load->view('form');

            if($_POST){

                $data_feature  = $_POST['feature'];

                $data = [];
                foreach($data_feature as $f_key => $f_value){

                    $data[$f_key]['var']= $this->Mdata->p_detail($f_value);

                }

            }   

        }

And your modal use as;

function p_detail($data_feature){

             $this->db->insert('feature',$data_feature);//inserts into a single column 
             return $this->db->insert_id();//returns last inserted id

}



回答2:


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.




回答3:


like what other people have suggested, change name="feature" to name="feature[]"

if you want to insert the values of $_POST['feature'] to multiple rows in your table, you could loop inserting

$feature_array = $_POST['feature'];
$feature_len = count($feature_array);
for ($i=0;$i<$feature_len;$i++) {
    p_detail($feature_array[$i]);
}

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



回答4:


You comment in Sagar Arora solution is that you want to insert it in rows instead of comma separated. For that, at first you have to change all checkboxs name from name="feature" to name="feature[]"

then in you model

$this->db->insert_batch('feature', $data_feature['feature']); 



回答5:


you have a big problem in your application design , if you Continue with this idea i think , you will have another problem in update and delete methods.

i advice you to create a feature table that can manage all your feature in your back office application and create intermediate table property_feature . because here you have many to many relationship between feature and property tables



来源:https://stackoverflow.com/questions/42525186/how-to-insert-multiple-fields-data-in-one-column-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!