无法将多选id引入数组并在codeigniter php中插入数据库

时间:2016-11-24 11:33:13

标签: php mysql arrays codeigniter

您好我正在使用多选下拉菜单来选择多个类别多选项工作正常但是在数组中打印时它只显示最后一个ID。不会将所有选定的记录都添加到数组中。

查看:

<div class="element">
        <label for="categoryname"><font color="black">Category Name</font></label>
        <?php
            $categorylist['']='--Select Category Name--';
            $category_id="id='category_id[]'  multiple='multiple' ";
            if($this->input->post('category_id')) $selected=$this->input->post('category_id');else $selected='';
            echo form_dropdown('category_id',$categorylist,$selected,$category_id);
        ?>
        <?php echo form_error('category_id', '<div class="error">', '</div>'); ?><br/><br/>     
    </div>

型号:

function insertblogs()
{
    $options = $this->input->post('category_id');
    $array = explode(",", $options);
    print_r($array);
    exit();
    $data=array(
    'category_id'=>$array,
    'blog_title'=>$this->input->post('blog_title'),
    'description'=>$this->input->post('description'),
    'position'=>$this->input->post('position')
    );
    $this->db->insert('blogs',$data);

1 个答案:

答案 0 :(得分:0)

型号:

function insertblogs()
{
    $options = $this->input->post('category_id');
    $array = implode(",", $options);

    $data=array(
    'category_id'=>$array,
    'blog_title'=>$this->input->post('blog_title'),
    'description'=>$this->input->post('description'),
    'position'=>$this->input->post('position')
    );
     if ( $_FILES AND $_FILES['image_path']['name'])
            {
                $file_name = $this->do_upload2();
                if(is_array($file_name)){
                    $error['imageerror'] = $file_name['error'];
                }
                else
                $data['image_path']=$file_name;
            }
            if(!isset($data['image_path']) && !isset($error['imageerror']))

        $error['imageerror'] ="Please Upload an image";

    if(isset($error))return $error;
   print_r($data);
        $this->db->insert('blogs',$data);

   }

查看:

<div class="element">
        <label for="categoryname"><font color="black">Category Name</font></label>
        <?php
            $categorylist['']='--Select Category Name--';
            $category_id="id='category_id[]'  multiple='multiple' ";
            if($this->input->post('category_id')) $selected=$this->input->post('category_id');else $selected='';
            echo form_multiselect('category_id[]',$categorylist,$selected,$category_id);

        ?>
        <?php echo form_error('category_id', '<div class="error">', '</div>'); ?><br/><br/>     
    </div>