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

2
  • 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 stackoverflow.com/questions/41304945/… stackoverflow.com/questions/41215624/… Commented Mar 8, 2017 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. Commented Mar 8, 2017 at 17:30

2 Answers 2

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

  }
Sign up to request clarification or add additional context in comments.

6 Comments

is there any way if i applied loop in model instead of controller
yes it is possible.You need to pass feature array $feature then use loop there.
did you solve it using above answer or any other problem.
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.
so i want user basic information insert one time but this is not happening due to foreach loop
|
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();
}

1 Comment

i want to insert values from 3 fields into a single column in database, can you help me with that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.