Re: Help in passing multiple HTML form options to SQL query <-- Newbie
| From: | Richard Chauvaux | Date: | Sun, 28 Oct 2001 02:28:47 +0000 |
| Subject: | Re: Help in passing multiple HTML form options to SQL query <-- Newbie | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-13776@lists.php.net to get a copy of this message | ||
first, the select tag in your form should be setup as an array: <select name="country[]" ... >
then the array $country[] will be available to your script on submittal.
a simple way to build your sql where clause would be to iterate through the array using a subscript variable:
$sql = "select * from tbl where ctry='$country[0]'";
for ($n=1; count($country) > $n; $n++) {
$sql.= " or ctry='$country[$n]'";
}
richard
TorrentUK wrote:
I am designing a ski web site and am presently trying to put together a resort database. I started the search script tonight and am quite pleased that it works. Where I am struggling is that I would like my visitors to be able to select multiple countries from the drop down list and for my query to pull back info on all those selected. When I try it it only pulls back the last one (naturally). Please advise on what would be the most efficient way of constructing the query. Please talk in easy speak as I am real new at this and am not a developer by any stretch of the imagination. Many Thanks Torrent Here is a cut down version of my code, the html form is at the bottom. <?php if ($search) { $sql = "SELECT rst_name, cty_name FROM resort_tbl WHERE cty_name='$country'"; $result = mysql_query($sql); if ($row = mysql_fetch_array($result)) { echo "<center><table border=1>\n";echo "<tr><td>Name</td><td>Country</td></tr>\n";do {printf("<tr><td>%s</td><td>%s</td>\n", $row["rst_name"],$row["cty_name"]);} while ($myrow = mysql_fetch_array($result));echo "</table></center>\n"; } } ?> <form name="resort" method="post" action="<?php echo $PHP_SELF?>"><p> Select country </font><br> <select name="Country" size="5" multiple> <option value="Andorra">Andorra</option> <option value="Austria">Austria</option> <option value="Bulgaria">Bulgaria</option> <option value="Finland">Finland</option> <option value="France">France</option> <option value="Italy">Italy</option> <option value="North America">North America</option> <option value="Norway">Norway</option> <option value="Romania">Romania</option> <option value="Slovenia">Slovenia</option> <option value="Spain">Spain</option> <option value="Sweden">Sweden</option> <option value="Switzerland">Switzerland</option> </select> <br> <input type="submit" name="search" value="Search"> </p> </form>