0

嗨,我已经能够使用 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);

        }
    }
}
?>

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

4

1 回答 1

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
}
于 2013-02-25T23:43:35.403 回答