奇怪的urldecode困境

时间:2011-11-02 15:31:45

标签: php codeigniter

我使用此urlencoded字符串

将变量传递到另一个页面
http://localhost:8888/proseat/index.php/configure/ve/Honda/Civic+%28sedan%29/2007

当我使用urldecode取回变量时,一切似乎都很好。我在这里使用codeigniter,所以在打印出解码后的变量之后我会看到以下内容:

Honda
Civic (sedan)
2007

当我关闭变量以用于sql时,我什么也得不回来。奇怪的是,如果我传递字符串“Civic(sedan)”,一切正常。

首先我编码网址:

function fr ()
{
    if ( isset($_POST) )
    {
        $make              = (urlencode($this->input->post('make')));
        $model              = (urlencode($this->input->post('model'))); 
        $year              = (urlencode($this->input->post('year')));   


        redirect('configure/ve/'.$make. '/' . $model. '/' .$year);


    }       
}

然后,我解码并传递给另一个函数进行处理。

function ve ($make, $model, $year) 
{
    if ( isset($make,$model,$year) )
    {

        $data['make']               = trim(urldecode($make));   
        $data['model']              = trim(urldecode($model));  
        $data['year']               = trim(urldecode($year));   

        $data['makes']              = $this->model_cars->getAllMakes(); //get all the years makes and models
        $data['models']             = $this->model_cars->getAllModels($data['make']);
        $data['years']              = $this->model_cars->getAllYears($data['make'], $data['model']);

        $this->load->view($this->session->userdata('language').'/includes/view_header',$tags);
        $this->load->view($this->session->userdata('language').'/configure/view_configure',$data);
        $this->load->view($this->session->userdata('language').'/includes/view_footer');

    }
    else
        //stuff 
}

变量转储:

        echo var_dump($data['make']).'<br>';
        echo var_dump($data['model']).'<br>';
        echo var_dump($data['year']).'<br>';

这是

的结果
string(5) "Honda" 
string(21) "Civic (sedan)" 
string(4) "2007" 

最后,这是由于某种原因返回NULL的函数。

function getAllYears ($make, $model) 
{  
    $result = NULL;
    $sql = "select distinct year from seatcover_listings where make=? and model=? order by year desc";

    $query = $this->db->query($sql, array(($make), ($model)));
    if ($query->num_rows() > 0) $result = $query->result_array();  //the format of the returned var will be $somevar['name']
    return $result;
}   

我知道该功能正常,因为如果我使用以下行,一切正常:

$this->model_cars->getAllYears($data['make'], 'Civic (sedan)');

我在使用urldecode之后尝试使用htmlspecialchars_decode但仍然没有。我无法相信我花了多少时间来解决这个问题。请帮帮我。

这是var_dump($ sql,$ query);

的结果
string(88) "select distinct year from seatcover_listings where make=? and model=? order by year desc" object(CI_DB_mysqli_result)#22 (8) { ["conn_id"]=> object(mysqli)#15 (17) { ["affected_rows"]=> int(0) ["client_info"]=> string(6) "5.1.44" ["client_version"]=> int(50144) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["field_count"]=> int(1) ["host_info"]=> string(25) "Localhost via UNIX socket" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(6) "5.1.44" ["server_version"]=> int(50144) ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(270) ["warning_count"]=> int(0) } ["result_id"]=> object(mysqli_result)#26 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) } ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(0) ["row_data"]=> NULL }

2 个答案:

答案 0 :(得分:0)

我认为问题在于变量值。请注意,您的“模型”变量打印为

string(21) "Civic (sedan)"

然而字符串“Civic(sedan)”只包含13个字符,因此它用其他东西填充,这使得它长21个字符。您需要弄清楚填充是什么,并在使用变量之前将其剥离。

答案 1 :(得分:0)

尝试为getAllYears方法提供默认值。另外,我不确定数组值的括号是否合法;请注意result_array()返回整个结果集;在代码中查看您的注释,您似乎要么在循环之后讨论检索值,要么使用row_array()来返回单行。

function getAllYears ($make = '', $model = '') 
{  
    $result = NULL;

    $sql = "SELECT DISTINCT year FROM seatcover_listings WHERE make=? and model=? ORDER BY year DESC";

    $query = $this->db->query($sql, array($make,$model));

    if ($query->num_rows() > 0) 
    {
        $result = $query->result_array();  //the format of the returned var will be $somevar['name']
    }
    return $result;
}   

您还可以使用AR构建条件查询:

$this->db->select('year');
$this->db->distinct();
$this->db->from('seatcover_listings');
if($make != '')
{
    $this->db->where('make',$make);
}
if($model != '')
{
    $this->db->where('model',$model);
}
$this->db->order_by('year','DESC');
$query = $this->db->get();
$result = $query->result_array();