2

I have a form with similar form input name which I would like to insert into different rows of database table. But when I submit form, the inputs with similar form input name don't get inserted.

Here is my view

<div class="row">

          <div class="col-xs-12">
            <div class="item-block">
              <div class="item-form">

                <button class="btn btn-danger btn-float btn-remove"><i class="ti-close"></i></button>

                <div class="row">
                  <div class="col-xs-12 col-sm-6">
                    <div class="form-group">
                      <input type="text" name="skills_name[]" class="form-control" placeholder="Skill name, e.g. HTML">
                    </div>
                  </div>

                  <div class="col-xs-12 col-sm-6">

                    <div class="form-group">
                      <div class="input-group">
                        <input type="text" name="skills_proficiency[]" class="form-control" placeholder="Skill proficiency, e.g. 90">
                        <span class="input-group-addon">%</span>
                      </div>
                    </div>

                  </div>
                </div>

              </div>
            </div>
          </div>

          <div class="col-xs-12 duplicateable-content">
            <div class="item-block">
              <div class="item-form">

                <button class="btn btn-danger btn-float btn-remove"><i class="ti-close"></i></button>

                <div class="row">
                  <div class="col-xs-12 col-sm-6">
                    <div class="form-group">
                      <input type="text" name="skills_name[]" class="form-control" placeholder="Skill name, e.g. HTML">
                    </div>
                  </div>

                  <div class="col-xs-12 col-sm-6">

                    <div class="form-group">
                      <div class="input-group">
                        <input type="text" name="skills_proficiency[]" class="form-control" placeholder="Skill proficiency, e.g. 90">
                        <span class="input-group-addon">%</span>
                      </div>
                    </div>

                  </div>
                </div>

              </div>
            </div>
          </div>

          <div class="col-xs-12 text-center">
            <br>
            <button class="btn btn-primary btn-duplicator">Add experience</button>
          </div>


        </div>

      </div>
    </section>
    <!-- END Skills -->

Controller:

function _get_skills_data(){

    $data4['skills_name'] = $this->input->post('skills_name', TRUE);
    $data4['skills_proficiency'] = $this->input->post('skills_proficiency', TRUE);
    $session_data = $this->session->userdata('logged_in');
    $data4['user_id'] = $session_data['user_id'];

    return $data4;

}

if ($this->form_validation->run($this) == FALSE)
            {
                    $this->add_resume();
            }
            else
            {

                    $data4 = $this->_get_skills_data();


                    $this->User_model->insert_resume('skills',$data4);

                    $this->session->set_flashdata('message', '<div class="alert alert-success">Resume Added</div>');


                    redirect('user/manage_resume', 'refresh');
            }

model:

function insert_resume($table, $data){

$query = $this->db->insert($table, $data);
return $this->db->insert_id();

}

Please what am i getting wrong,I have read some questions on this which isn't working for me. Thank you!

5
  • for firstly forget CI try to make the form like in core php and html then post the form, after that try in ci Commented Nov 7, 2016 at 12:13
  • In model write echo $this->db->last_query() before return . See what query is built. Commented Nov 7, 2016 at 12:13
  • 1
    The post skills_name[] is coming through as an array, you need to loop the array and build the insert script that way. At the moment I think you are trying to insert an array into the database. Commented Nov 7, 2016 at 12:14
  • Yes I am trying to insert an array into different database rows. I've tried looping through, and I get Array to string conversion error Commented Nov 7, 2016 at 12:23
  • echo $this->db->last_query() didn't work Commented Nov 7, 2016 at 12:29

1 Answer 1

2

Based on Blinkydamo's Comment you need to loop over your post data because you try to insert these as an array

try something like this:

function _get_skills_data()
{
    $arrData = array();
    $arrPostSkillsName = $this->input->post('skills_name', TRUE);
    $arrPostSkillsProficiency = $this->input->post('skills_proficiency', TRUE);
    $session_data = $this->session->userdata('logged_in');

    $count = count($arrPostSkillsName);

    for($i=0;$i<$count;$i++)
    {
        $arrItemData = array();
        $arrItemData['skills_name'] = $arrPostSkillsName[$i];
        $arrItemData['skills_proficiency'] = $arrPostSkillsProficiency[$i];
        $arrItemData['user_id'] = $session_data['user_id'];

        $arrData[] = $arrItemData;
    }

    return $arrData;
}

and your second controller function snippet

if ($this->form_validation->run($this) == FALSE)
{
    $this->add_resume();
}
else
{
    $arrData = $this->_get_skills_data();
    foreach($arrData AS $arrItemData)
    {
        $this->User_model->insert_resume('skills',$arrItemData);
    }
    $this->session->set_flashdata('message', '<div class="alert alert-success">Resume Added</div>');
    redirect('user/manage_resume', 'refresh');
}
Sign up to request clarification or add additional context in comments.

3 Comments

But it adds one additional empty input into the database where could that be coming from
i've no idea - you've to print out the array - but i think the problem is, you leave the fields in your html form empty; maybe you do a check on the _get_skills_data function whether the data are set or not
Thanks @sintakonte

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.