Focus Ifeanyi Focus Ifeanyi - 1 month ago 5
MySQL Question

Insert form inputs with the same input name into database rows using codeigniter

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!

Answer

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');
}