1

I am submitting the form and getting the url like http://localhost/web/test.php?cuisine=1&cuisine=2&cuisine=3.

I want to get the result like echo $_GET['cuisine']; and the output should be "1,2,3" / $rt = "1,2,3"; i want to use it in query (select * from table where id in ($rt) )

My Form: No Cuisine Restriction

$result = $db->query("
SELECT
    *
FROM
    cuisines
WHERE
    status=1
order by id
");
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
?>
<option value="<?php echo $rs["id"]; ?>"><?php echo $rs["ename"]; ?></option>
<?php } ?>
</select>

</form>

I have tried the below but it is taking only the last id#3, when i select only the one not multiple it works fine:

$temp = $_GET['cuisine'];
$thelist    =   implode(",",$temp);

I have tried http://localhost/web/test.php?id[]=1&id[]=2&id[]=3 & printing the result it gives me this Array ( [0] => 1 [1] => 2 ) but how to use it in WHERE id IN (....); i tried bu can not.

Fahad Almehaini
  • 233
  • 3
  • 18

1 Answers1

0

As suggested in the comments by chris85 and Craig. Change the name of your select field to cuisine[].

<?php
var_dump($_GET);
?>
<form action="">
    <select name="cuisine[]" multiple>
        <option value="">None</option>
        <?php $cuisines = ['indian', 'english', 'thai', 'italian']; ?>
        <?php foreach($cuisines as $key => $cuisine) { ?>
            <option value="<?php echo $key; ?>"><?php echo $cuisine; ?></option>
        <?php } ?>
    </select>
    <input type="submit">
</form>

If I select 'english' and 'thai', output reads:

array (size=1)
  'cuisine' => 
    array (size=2)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • Side note: I don't think that multiple selects are that user friendly. Perhaps consider using check boxes. – Progrock Sep 15 '16 at 03:26