将下拉列表值传递给codeigniter中的控制器

时间:2013-02-25 15:55:36

标签: php html codeigniter drop-down-menu

您已经能够使用codeigniter将图像路径上传到数据库,但我似乎无法将类别选项传递给数据库。我有2个选项的下拉菜单,然后我想将它传递给控制器​​,所以我可以将它插入数据库。

这是我的代码:

我的观点

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" />
<br />
<select name="album" id="album"> // my dropdown list
    <option value="staff">Album Staff</option>
    <option value="gallery">Gallery</option>
</select>
<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

这是我的控制器

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $config['upload_path'] = './assets/images';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;
        $config['encrypt_name'] = FALSE;
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
            $upload_info=$this->upload->data();
            $insert_data = array(
            'imgfilename' => $upload_info['file_name'],
            'imgfilepath' => $upload_info['file_path'],
            'album' => $this->input->post('album') //trying to get the value from select in view
            );
            $this->db->insert('db_images', $insert_data);

        }
    }
}
?>

应该怎么做?任何解决方案?

1 个答案:

答案 0 :(得分:0)

您是否尝试使用mysql_real_escape_string($_POST["album"])查看其工作原理?将global_xss_filter设置为true可以保护/影响一些事情。奇怪的是,你不能总是说出干扰你代码的是什么。这就是为什么你应该有多个选项。尝试上面的行,看看你是否得到了结果。如果你这样做,你可以这样做:

$album = "";
$albums = array("gallery", "staff"); //restrict types of allowed albums
$str = mysql_real_escape_string($_POST["album"]); //clean up the string

if(in_array($str, $albums)){
$album = $str; //get the album to send to database
}