奇怪的 urldecode 困境
我使用这个 urlencoded 字符串将变量传递到另一个页面
http://localhost:8888/proseat/index.php/configure/ve/Honda/Civic+%28sedan%29/2007
,当我使用 urldecode 取回变量时,一切看起来都很好。我在这里使用 codeigniter,所以在打印出解码的变量后,我看到以下内容:
Honda
Civic (sedan)
2007
当我将变量传递给 sql 时,我什么也没有得到。奇怪的是,如果我传递字符串“Civic(轿车)”,一切都会正常。
首先,我对 url 进行编码:
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 }
I am passing variables to another page with this urlencoded string
http://localhost:8888/proseat/index.php/configure/ve/Honda/Civic+%28sedan%29/2007
and when I use urldecode to get back the variables everything seems fine. I am using codeigniter here, so after printing out the decoded variables I see the following:
Honda
Civic (sedan)
2007
When I pass the variables off to be used for sql I get nothing back. The weird thing is if I pass the string "Civic (sedan)" everything works fine.
First I am encoding the url:
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);
}
}
Then, I decode and pass off to another function for processing.
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
}
A dump of the variables:
echo var_dump($data['make']).'<br>';
echo var_dump($data['model']).'<br>';
echo var_dump($data['year']).'<br>';
This is the result from that
string(5) "Honda"
string(21) "Civic (sedan)"
string(4) "2007"
Finally, this is the function that returns NULL for some reason.
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;
}
I know the function works fine because if I Use the following line everything works fine:
$this->model_cars->getAllYears($data['make'], 'Civic (sedan)');
I tried using htmlspecialchars_decode before after using urldecode but still nothing. I can't beleive how much time I have spent trying to figure this out. Please help me out here.
Here is the result to 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 }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题出在变量值上。请注意,您的“model”变量打印为
字符串“Civic (sedan)”,但仅包含 13 个字符,因此它用其他内容填充,这使其长度为 21 个字符。您需要弄清楚填充是什么,并在使用变量之前将其剥离。
I think the problem is in the variables values. Notice that your 'model' variable is printed as
however string "Civic (sedan)" contains only 13 characters, therefore it's padded with something else, which makes it 21 chars long. You need to figure out what that padding is and strip it before using the variable.
尝试为 getAllYears 方法提供默认值。另外,我不确定数组值周围的括号是否合法;请注意 result_array() 返回整个结果集;查看代码中的注释,似乎您要么谈论在循环后检索值,要么使用 row_array() 代替,它返回单行。
您还可以使用 AR 构建条件查询:
Try giving default values to the getAllYears method. Also, I'm not sure if parenthesis around array values are legal or not; beware that result_array() returns the whole result set; looking at your comment in the code it seems either you talk about retrieving a value after a loop, or by using row_array() instead, which returns a single row.
You could also build a conditioned query using AR: