0

我有另一个表格来上传具有多个值 (10) 的 1 个产品的详细信息,但假设我们选择 New Arrival、Black、White 并上传到数据库。所以现在我的数据库将字段名称为“类别”,其值为新到货、黑色、白色。

现在我想做一个搜索功能,但每当我尝试运行它时,它就不会显示结果。所以我做了2条记录,其中:

具有字段类别和值“新到货”的第一个记录 第二个具有字段类别和值“新到货,黑色,白色”的记录

当我再次尝试运行我的代码时,它确实返回了第一条记录的结果,我尝试为几行复制相同的记录,结果它只能返回类别字段只有 1 个值的结果。

以下只是我的代码的一小部分:

我为类别字段添加记录表单输入是:

add_new.html

<select name="category[]" size="10" multiple="multiple">
<options>New Arrival</options>
<options>Black</options>
<options>White</options>
</select>

add_process.php

$category = implode(", ", $_POST['category']);
$strSQL = "INSERT INTO products_list (category) VALUES ('$category')";

search_form.html

<input type="text" name="search_text" />
<input type="submit" name="submit" />

search_process.php

$category = mysql_real_escape_string($_POST['product_category']);
$select = mysql_query("select image1, image2, image3, image4 from products_list WHERE category IN ('new arrival') ORDER BY id ASC");

while($row=mysql_fetch_array($select)) {

    echo $row['image1'];
    echo $row['image2'];
    echo $row['image3'];
    echo $row['image4'];

}

重复我的问题,如何获得该类别字段中包含(期望值)的行的结果?

下一个问题是,类别值仅在数据库中存储为“新到货”,如果我只输入“到货”而不是全名,如何获得返回结果?目前,如果我只输入“到达”,它也不会返回任何结果。

希望你们明白我想说什么。提前谢谢各位。

4

2 回答 2

1

塞尔 说:

$select = mysql_query("select image1, image2, image3, image4 from products_list WHERE category like '%new arrival%' ORDER BY id ASC");
于 2012-06-20T06:58:32.573 回答
1

为了方便参考,我把解释放在这里。

$catsearch = $_POST["category"]; 
$keywords = explode(' ', $catsearch);  //if keywords entered is "white black new", will convert to array of "white","black","new". The delimiter here is the space.
$keywordsBits = array(); 
foreach ($keywords as $keyword) { 
          $keyword = trim($keyword); 
          if (!empty($keyword)) { 
                $keywordsBits[] = "category LIKE '%$keyword%'"; 
          } 
} 

$result = mysql_query("SELECT * FROM products_list WHERE ".implode(' OR ', $keywordBits));

这将导致类似的查询

SELECT * FROM products_list WHERE category LIKE '%white%' OR category LIKE '%black%' OR category LIKE '%new%'

如果你想用“,”分隔关键字,你可以使用

$keywords = explode(',', $catsearch);  //if keywords entered is "white,black,new arrival", will convert to array of "white","black","new arrival". The delimiter here is the comma.

谢谢。

于 2012-06-20T07:53:37.547 回答