问题
I'd like some help please.
I'm sending with a GET request from a multiple select field the selected values to my proccess page (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>
I get the selected locations on my url like this
location[]=103002000&location[]=103003000
and in the archive.php I'm trying to fetch data from the database like this
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.")";
}
but I'm getting the following error: Error: Unknown column 'Array' in 'where clause'.
How can I fix this?
回答1:
try with as below it might be simple
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")
来源:https://stackoverflow.com/questions/29119484/cant-get-the-values-from-multiple-select-with-get-request