Can't get the values from multiple select with $_GET request

家住魔仙堡 提交于 2019-12-11 23:50:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!