0

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

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
Harris Khan
  • 247
  • 10
  • 26
  • 1
    this is not the way you should be using a RDBMS. There are thousands of questions here from people who took this path and are now stuggling Don't save CSV in a column http://stackoverflow.com/questions/41304945/best-type-of-indexing-when-there-is-like-clause/41305027#41305027 http://stackoverflow.com/questions/41215624/sql-table-with-list-entry-vs-sql-table-with-a-row-for-each-entry/41215681#41215681 – e4c5 Mar 08 '17 at 13:52
  • put "echo $this->db->last_query();die;" after your insert query. It will display your query and check where you going wrong. – Ashish Tiwari Mar 08 '17 at 17:30

2 Answers2

2

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();

  }
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
  • is there any way if i applied loop in model instead of controller – Harris Khan Mar 08 '17 at 14:21
  • yes it is possible.You need to pass feature array `$feature ` then use loop there. – Hikmat Sijapati Mar 08 '17 at 14:23
  • did you solve it using above answer or any other problem. – Hikmat Sijapati Mar 08 '17 at 14:24
  • actually my form insert data in two tables, one table is for features and other are basic information ?? this one is working but the problem is my basic information is inserting multiple times. for instance if user check on 3 features the basic information also insert 3 dublicate data. – Harris Khan Mar 08 '17 at 14:32
  • so i want user basic information insert one time but this is not happening due to foreach loop – Harris Khan Mar 08 '17 at 14:35
  • then take first `feature `using `feature[0]` and insert into first table and take second value using `feature[1]` and insert into second table.No need to use foreach loop. – Hikmat Sijapati Mar 08 '17 at 14:49
2

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();
}
Firman
  • 463
  • 2
  • 7
  • 21