0

我有一个带有 html 表单的搜索引擎,它将数据发送到 PHP 脚本以查询 MySQL 数据库。在 html 表单中,我有一个允许多项选择的选项。到目前为止,我在 HTML 中的名称后放置了方括号,以使其成为一个数组。但我认为 PHP 存在问题,因为结果不正确。

HTML

<select multiple="multiple" name='category[]'>
      <option>Literature</option>
      <option>History</option>
      <option>Science</option>
      <option>Fine Arts</option>
      <option>Trash</option>
      <option>Mythology</option>
      <option>Phylosophy</option>
      <option>Social Science</option>
      <option>Religion</option>
      <option>Geography</option>
  </select>

搜索.php

$button = $_GET ['submit'];
$search = $_GET ['search'];

}

if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b><em>$search</em></b> and ";
mysql_connect("fake","fake","fake");
mysql_select_db("quinterestdb");}

mysql_real_escape_string($search);

$search_exploded = explode (" ", $search);

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="Answer LIKE '%$search_each%'";
else
$construct .="AND Answer LIKE '%$search_each%'";

}

$cat = $_GET ['category'];
$comma_separated = implode("','", $cat);

$constructs ="SELECT * FROM tossups WHERE $construct AND Category IN('$comma_separated')";
$run = mysql_query($constructs);

当我使用搜索引擎时,脚本运行得很好,但仍有未选择类别的结果。任何想法?

4

1 回答 1

2

那不是办法。应该是这样的,带有 VALUE

<select multiple="multiple" name='category[]'>
      <option value="v1">Literature</option>
      <option value="v2">History</option>
...
  </select>
于 2013-06-17T06:42:33.253 回答