😕 Could some one help me with the query to get multiple selections from a list box to filter records in MySQL. For example, in the sample files below, when multiple items are selected, the query returns only the last selection (one record):
================= form.php =================
<form action="results.php?search=true" method="get" name="form" target="_self" id="form">
<p>
<select name="fruits[]" size="5" multiple>
<option value="apples">Apples</option>
<option value="oranges">Oranges</option>
<option value="pears">Pears</option>
<option value="grapes">Grapes</option>
<option value="mangos">Mangos</option>
</select>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
============================================
================= results.php =================
<?php
$foo = $_GET['fruits'];
if (count($foo) > 0) {
for ($i=0;$i<count($foo);$i++) {
echo "<li>$foo[$i] \n";
mysql_select_db($database_conn, $conn);
$query_Recordset1 = sprintf("SELECT * FROM test WHERE name LIKE '%%%s%%'", $foo[$i]);
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
}
}
?>
<html><head></head><body>
<table width="200" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>testID</td>
<td>name</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['testID']; ?></td>
<td><?php echo $row_Recordset1['name']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
</body>
</html>
<?php
?>
I hope someone can modify the code to:
- extract the multi-selected data from the list box;
- SQL query/filter records in a MySQL DB, and
- display the matching records as a repeating region.
The list box is one of several other form elements used on the same form for search - all functional.
I would appreciate any help. Thanks.