我有另一种表格来上传具有多个值 (10) 的 1 个产品的详细信息,但假设我们选择新到货、黑色、白色并上传到数据库。所以现在我的数据库将字段名称为"类别",其值为新到货、黑色、白色。
现在我想做一个搜索功能,但每当我尝试运行它时,它都不会显示结果。所以我做了 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'];
}
重复我的问题,如何获取包含该类别字段中(所需值)的行的结果?
下一个问题是,类别值仅作为"新到货"存储在数据库中,如果我只输入"到达"而不是全名,如何获得返回结果?目前,如果我只输入"到达",它也不会返回任何结果。
希望你们明白我想说什么。提前感谢伙计们。
为了便于参考,我把解释放在这里。
$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.
谢谢。
塞尔 说:
$select = mysql_query("select image1, image2, image3, image4 from products_list WHERE category like '%new arrival%' ORDER BY id ASC");