2013-02-25 73 views
0

您好我已經能夠使用codeigniter上傳圖像路徑到數據庫,但我似乎無法將類別選項傳遞給數據庫。我有2選項的下拉菜單,然後我想將它傳遞給控制器​​,所以我可以將其插入數據庫。將下拉列表值傳遞給codeigniter中的控制器

這裏是我的代碼:

我看來

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

     } 
    } 
} 
?> 

應該怎樣做呢?任何解決方案

+0

做的var_dump($這個 - >輸入 - >後()),並粘貼在這裏的結果 – 2013-02-25 16:28:34

+0

的print_r($ insert_data);並檢查數組元素 – Arunu 2013-02-25 17:12:29

回答

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 
} 
相關問題