1

I have face a problem. I want to insert multi checked value in multi row in mysql data table.... But When i insert it store all in one row... How can i fix it... My Model is:

    public function apply_for_work_check()
    {
    $variable = $this->input->post('Infrashtructure');
     $data = array(
    'id' =>null,
    'works_id' => 1,
    'infrashtructure_name' => json_encode(implode(",", $variable)),
    );
    $result=$this->db->insert('infrashtructure_txn_info',$data);
    if($result)
    {
    return TRUE;
    }
    else
    {
    return FALSE;
    }
    }
2
  • Do insert for each value in $variable not once with implode() Commented Mar 7, 2017 at 10:59
  • Please show the content of $variable using print_r($variable) Commented Mar 7, 2017 at 11:09

3 Answers 3

1
public function apply_for_work_check()
{
 $result=array();
 $variable = $this->input->post('Infrashtructure');
 foreach($variable as $var){
     $data = array('id' =>null,'works_id' => 1,
                   'infrashtructure_name' => json_encode($var));
     $result[]=$this->db->insert('infrashtructure_txn_info',$data);
 }

 if(count($result)==array_sum($result)) {#checks if all was TRUE
   return TRUE;
 } else {
   return FALSE;
 }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Change your method as below:

public function apply_for_work_check()
{
    $variable = $this->input->post('Infrashtructure');
    foreach ($variable as $key => $value) {
        $data = array(
            'id'                   => null,
            'works_id'             => 1,
            'infrashtructure_name' => $value,
        );
        $result = $this->db->insert('infrashtructure_txn_info', $data);
    }
    if ($result) {
        return true;
    } else {
        return false;
    }
}    

Comments

0
$data = explode(",", $variable);

After this.

'infrashtructure_name' => json_encode(implode(",", $data))

Comments

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.