CodeIgniter Forums

Full Version: [Help ASAP PLEASE] form_multiselect insert...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]chazy (aldever calvo)[/eluser]
hello.. i need help regarding form_multiselect...
whenever i tried to insert the multiple selected value into the db, it always gives me error:
Code:
A Database Error Occurred
Error Number:

ERROR: invalid input syntax for integer: "13[]" LINE 1: ..."ch_act_mat_mat_id", "ch_act_mat_act_id") VALUES ('13[]', '6... ^

INSERT INTO "choy_activity_materials" ("ch_act_mat_mat_id", "ch_act_mat_act_id") VALUES ('13[]', '6')

heres my model:
Code:
function AddSetMaterial($actNum, $actMat)
    {
        $data = array(
                'ch_act_mat_mat_id'    => $actMat.'[]',
                'ch_act_mat_act_id'    => $actNum
            );
        
        return $this->db->insert('choy_activity_materials', $data);    
    }
function get_mat_dropdown()
    {
        $this->db->from('choy_material');
        $this->db->order_by('choy_mat_name');
        $result = $this->db->get();
        
        $return = array();
        if($result->num_rows() > 0)
        {
                $return[''] = 'Please select (Hold [ctrl] for multiple selection)';
            foreach($result->result_array() as $row)
            {
                $return[$row['ch_mat_id']] = $row['choy_mat_name'];
            }
        }
        return $return;
    }

view
Code:
<table>
    <tr>
        <th colspan="2">
        SET ACTIVITY MATERIALS</th>
    </tr>
    <tr>&lt;?php $actNum = $this->input->post('actNum');?&gt;
        <td><label>*Activity Number : <span class="small">Select from existing activity</span></label></td>
        <td>&lt;?php echo form_dropdown('actNum', $activity, 'style="height:25px;"')?&gt;</td>
    </tr>
        <tr>
            <td></td>
            <td>&lt;?php echo form_error('actNum')?&gt;;</td>
        </tr>
    <tr>&lt;?php $actMat = $this->input->post('actMat[]');?&gt;
        <td><label>*Material :<span class="small">Choose from the list of existing materials (Sort Alphabetically)</span></label></td>
        <td>&lt;?php echo form_multiselect ('actMat[]', $material, $actMat, 'style=height:200px;"')?&gt;</td>
    </tr>
        <tr>
            <td></td>
            <td>&lt;?php echo form_error('actMat')?&gt;</td>
        </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&lt;?php echo form_submit('', 'Set Activity Mterials');?&gt;</td>
    </tr>
</table>


controller
Code:
function setmaterials()
    {    
        $activity = $this->activities_model->get_actnum_dropdown();
        $data['activity'] = $activity;
        
        $material = $this->activities_model->get_mat_dropdown();
        $data['material'] = $material;
        
        //Validate Form
        $this->form_validation->set_rules('actNum', 'Activity Number', 'trim|required');
        $this->form_validation->set_rules('actMat', 'Material', 'trim|required');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('activities/activities_setmaterials_form', $data);
        }
        
        else
        {
            //The form is validated
            $actNum = $this->input->post('actNum');
            $actMat = $this->input->post('actMat');
      
            $this->activities_model->AddSetMaterial($actNum, $actMat);
}}

PLease help me... school deadline is almost up... T.T

El Forum

[eluser]Roy MJ[/eluser]
Ive done this multiple select box using implode and explode functions. U join together all the selected values separated by a comma or something and u can use the explode function to separate the imploded values too. Its much simpler than using arrays and stuff. Very simple and effective..

El Forum

[eluser]Ochetski[/eluser]
Your error is that "$actMat.'[]';" is not a valid value for a int (as defined on DB).
You should change the collumn type on DB to varchar or text.

@Roy's idea is also great. I use it my self and works great, you just have to make a function to create the array and another to convert it into a string when needed.

El Forum

[eluser]chazy (aldever calvo)[/eluser]
it's good now,... thanks... i changed the datatype of my table column form integer to varchar...
i hope theres another way of storing multiple int values... because i wanted to store the integer values per row in the column.

El Forum

[eluser]defectivereject[/eluser]
same here as in other thread, maybe the ", " is the issue? try removing the space after the comma in your model

El Forum

[eluser]Ochetski[/eluser]
Well. i guess "ch_act_mat_act_id" is an ID from another table and you want to store a lot of "ch_act_mat_mat_id"'s
you can change the collumn to int and insert a row per value.

Something like this:

Code:
$ch_act_mat_mat_id = $this->input->post('ch_act_mat_mat_id');
if(is_array($ch_act_mat_mat_id) && count($ch_act_mat_mat_id) > 0)
{
    foreach($ch_act_mat_mat_id as $key => $val)
    {
        $this->db->insert('YOUR_TABLE', array(
            'ch_act_mat_act_id' => $this->input->post('ch_act_mat_act_id'),
            'ch_act_mat_mat_id' => $val
        ));
    }
}

You will just have to select all with the same act_id instead of exploding a string into ints.
=D

El Forum

[eluser]chazy (aldever calvo)[/eluser]
nice.. thanks ^_^ i'll try this one...
yup... both those ch_act_mat_mat_id and ch_act_mat_act_id are both foreign keys of the table activity_materials in which i will pull the serial of activity_id of activity_table and the material_id of material_table into this table..

El Forum

[eluser]chazy (aldever calvo)[/eluser]
[quote author="Ochetski" date="1297462022"]Well. i guess "ch_act_mat_act_id" is an ID from another table and you want to store a lot of "ch_act_mat_mat_id"'s
you can change the collumn to int and insert a row per value.

Something like this:

Code:
$ch_act_mat_mat_id = $this->input->post('ch_act_mat_mat_id');
if(is_array($ch_act_mat_mat_id) && count($ch_act_mat_mat_id) > 0)
{
    foreach($ch_act_mat_mat_id as $key => $val)
    {
        $this->db->insert('YOUR_TABLE', array(
            'ch_act_mat_act_id' => $this->input->post('ch_act_mat_act_id'),
            'ch_act_mat_mat_id' => $val
        ));
    }
}

You will just have to select all with the same act_id instead of exploding a string into ints.
=D[/quote]

wow! thank you so much! it worked! Smile
at last! it's hard to be working alone with my undergrad thesis.
thanks for helping me...

El Forum

[eluser]Ochetski[/eluser]
Happy to know it worked =D
Good luck with your school deadline.

El Forum

[eluser]chazy (aldever calvo)[/eluser]
[solved by Ochetski]
Pages: 1 2