2

I try to insert my checkbox data in CodeIgniter. but data did not inserted in the database. here is my view file:

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

I am trying to use implode to convert the array into the string, but then I don't how to add in $data array, so they inserted in together

here is my controller:

     public function save()
     {
       $this->load->model('Partner_model');
       $feature = $this->input->post('feature');
      $fea=array(
             'feature'=>json_encode(implode(",",$feature)) 
                   );

      $user_data= array(
     'pname' => $this->input->post('pname'),
     'type' => $this->input->post('type'),
     'address' => $this->input->post('address'),
     'about' => $this->input->post('about'),
     'city' => $this->input->post('city'),
      'code' => $this->input->post('code')
     );
    if($this->Partner_model->save($user_data,$fea))
   {
       $msg = "save sucesss" ;
   }
   else
   {
       $msg = "not save";
   }

   $this->session->set_flashdata('msg', $msg);
   $this->load->view('partner_profile');
 }

& here is my model:

 public function save($data,$fea)
  {
     return $this->db->insert('property', $data,$fea);
  }

2 Answers 2

2

Your model is faulty.

You are passing three arguments to insert() but the third you use is not appropriate. That argument should be a boolean that indicates whether to escape values and identifiers or not. You need to incorporate $fea into $data which should probably be done in the controller.

There is an easier way to create the array $user_data since it is essentially a copy of $_POST just use $this->input->post().

Also, there is no obvious reason why you use json_encode. Unless you need it that way when you retrieve it from the DB there is no reason to bother with it. Consider removing json_encode.

First, change the model

public function save($data)
{
    return $this->db->insert('property', $data);
}

Here's a revised save method

public function save()
{
    $this->load->model('Partner_model');
    $user_data = $this->input->post(); //makes a copy of $_POST
    $feature = $this->input->post('feature');
    if($feature) //because $feature will be null if no boxes are checked
    {
        $user_data['feature'] = json_encode(implode(",", $feature));
    }

    $msg = $this->Partner_model->save($user_data) ? "save sucesss" : "not save";

    $this->session->set_flashdata('msg', $msg);
    $this->load->view('partner_profile');
}

An explanation as requested via comments.

A call to $this->input->post('pname') returns the value of $_POST['pname'] if it is exists, but returns null if it does not exist.

When you create $user_data you make six calls to $this->input() with a different "key" each time to make a copy of $_POST.

$this->input->post() without any arguments returns the whole $_POST array. (See documentation)

$user_data = $this->input->post(); 

Makes a copy of $_POST using one line of code. It will include $_POST['feature'] if any boxes are checked, but $_POST['feature'] will not be set if no boxes are checked.

There are two ways to test if any boxes were checked. First we can test if isset($_POST['feature']) == true or we can test if $this->input->post('feature') == true. I use the second with the call

 if($feature)

Which is pretty much the same as any of the following lines

   if($feature != false)...
   if($feature != null)...
   if( ! empty($feature))...
   if( ! is_null($feature))...

In other words, if($feature) evaluates as true if $feature is set and is anything except null, false, 0, "0", "" (an empty string), array() (an empty array)

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

4 Comments

I don't understand what you are trying to say about makes copy of $_POST,I change my model & also a controller but maybe i do it wrong:
controller: $user_data= $this->input->post(); $feature = $this->input->post('feature'); if($feature) //because $feature will be null if no boxes are checked { $user_data['feature'] = implode(",", $feature); } $user_data= array( //other data post);
I've added more details. Feel free to ask more questions.
BTW, @SimbuDev and I give the same answer except I use fewer lines of code to get the same outcome. Plus mine takes into account the case where no boxes are checked.
1
public function save()
 {
   $this->load->model('Partner_model');
   $feature = $this->input->post('feature');

   $user_data= array(
     'pname' => $this->input->post('pname'),
     'type' => $this->input->post('type'),
     'address' => $this->input->post('address'),
     'about' => $this->input->post('about'),
     'city' => $this->input->post('city'),
     'code' => $this->input->post('code'),
     'feature'=>json_encode(implode(",",$feature))
  );
  if($this->Partner_model->save($user_data)){
     $msg = "save sucesss" ;
  }else{
     $msg = "not save";
  }
  $this->session->set_flashdata('msg', $msg);
  $this->load->view('partner_profile');
}

model file should be :

public function save($data) {
  return $this->db->insert('property', $data);
}

13 Comments

which datatype are you using for 'feature' column in 'property' table? Can you send the error message if any?
varchar data type
Try to print the $user_data variable before sending it to model. It should work fine If it prints the value for 'feature'. Also try to send without 'feature' column in $user_data array. There is no fault in the above code. Kindly tell us the error message you got from model using the below code. print_r($this->db->_error_message());
Then print your $feature variable and check whether the $feature variable is single dimensional or multidimensional array
Remove json_encode and try. I think @DFriend edited version is fine. Can you try 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.