Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

php - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...