无法使用 $_GET 请求从多个 select 获取值
Can't get the values from multiple select with $_GET request
我需要一些帮助。
我正在使用来自多个 select 字段的 GET 请求将 selected 值发送到我的进程页面 (archive.php)
<form id="form1" class="four columns" action="archive.php" method="get" name="form1">
<select id="select2" multiple="multiple" name="location[]">
<option value="103001000">value1</option>
<option value="103002000">value2</option>
<option value="103003000">value3</option>
<option value="103004000">value4</option>
</select>
我在 url 上得到了 selected 的位置,就像这样
location[]=103002000&location[]=103003000
并且在 archive.php 我正在尝试像这样从数据库中获取数据
if (( isset($_GET['location']) && !empty($_GET['location']) )) {
die(var_dump($_GET['location'])); // the var_dump doesn't return an array at all
$loc = implode(', ', $_GET['location']);
$sql="SELECT * FROM locations WHERE AreaID IN (". $loc.")";
}
但我收到以下错误:错误:'where clause' 中的未知列 'Array'。
我该如何解决这个问题?
尝试如下可能很简单
if (( isset($_GET['location']) && !empty($_GET['location']) )) {
die(var_dump($_GET['location'])); // the var_dump doesn't return an array at all
//$loc = implode(', ', $_GET['location']);
$_GET['location'] = array('first'=>'loc1','second'=>'loc2','third'=>'loc3');
$loc = implode('","', $_GET['location']);
$loc ='"'.$loc.'"';
$sql='SELECT * FROM locations WHERE AreaID IN ('.$loc.')';
echo $sql;
}
result query
SELECT * FROM locations WHERE AreaID IN ("loc1","loc2","loc3")
我需要一些帮助。
我正在使用来自多个 select 字段的 GET 请求将 selected 值发送到我的进程页面 (archive.php)
<form id="form1" class="four columns" action="archive.php" method="get" name="form1">
<select id="select2" multiple="multiple" name="location[]">
<option value="103001000">value1</option>
<option value="103002000">value2</option>
<option value="103003000">value3</option>
<option value="103004000">value4</option>
</select>
我在 url 上得到了 selected 的位置,就像这样
location[]=103002000&location[]=103003000
并且在 archive.php 我正在尝试像这样从数据库中获取数据
if (( isset($_GET['location']) && !empty($_GET['location']) )) {
die(var_dump($_GET['location'])); // the var_dump doesn't return an array at all
$loc = implode(', ', $_GET['location']);
$sql="SELECT * FROM locations WHERE AreaID IN (". $loc.")";
}
但我收到以下错误:错误:'where clause' 中的未知列 'Array'。
我该如何解决这个问题?
尝试如下可能很简单
if (( isset($_GET['location']) && !empty($_GET['location']) )) {
die(var_dump($_GET['location'])); // the var_dump doesn't return an array at all
//$loc = implode(', ', $_GET['location']);
$_GET['location'] = array('first'=>'loc1','second'=>'loc2','third'=>'loc3');
$loc = implode('","', $_GET['location']);
$loc ='"'.$loc.'"';
$sql='SELECT * FROM locations WHERE AreaID IN ('.$loc.')';
echo $sql;
}
result query
SELECT * FROM locations WHERE AreaID IN ("loc1","loc2","loc3")