【问题标题】:Filter mysql results based on multiselect box根据多选框过滤mysql结果
【发布时间】:2015-04-01 10:36:37
【问题描述】:

我正在尝试根据多选框中的值过滤 mysql 表,以便它返回已选择值的所有结果。例如这是我的多选框:

<select name="gift[]" id="gift" multiple="multiple" onchange="change()">
            <option value="him">Him</option>
            <option value="her">Her</option>
            <option value="kids">Kids</option>
            <option value="teens">Teens</option>
            <option value="mothersday">Mothers Day</option>
            <option value="fathersday">Fathers Day</option>
            <option value="valentines">Valentines Day</option>
            <option value="gadgets">Gadgets</option>
            <option value="secretsanta">Secret Santa</option>
</select>

如果用户选择“him”和“gadgets”,它应该返回 mysql 表中包含“him”或“gadgets”的所有结果。

mysql 表将每个产品的数据作为字符串存储在单个列中(即,在“礼物”列下,它可能是他,她,孩子,小工具,或者另一行可能是她,小工具。使用上面的示例我希望它返回两行)。

结果由 AJAX 返回,我尝试使用 WHERE IN 子句但无法使其正常工作。这是我的代码(上面已经有 HTML 选择框):

AJAX 注意:为简单起见,并非完整代码

function change(){
var giftarray = new Array();
$.each($("#gift option:selected"), function(){            
        giftarray.push($(this).val());
 });

 var gift = "'" + giftarray.join("','") + "'";

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("product").innerHTML=xmlhttp.responseText;
}
}

var url="results.php"
url=url+"&gift="+gift;

xmlhttp.open("GET",url,true);
xmlhttp.send();
}

Results.PHP 注意:为简单起见,并非完整代码

$gift=$_GET['gift'];
$gift=str_replace("\\","",$gift);
$sql= "SELECT * FROM $productstable WHERE gift IN ($gift)";
$result = mysql_query($sql) or die ('Unable to run query:'.mysql_error());
while($item = mysql_fetch_array($result))
{
code to view products here
}
<? } mysql_close()  ?>

Mysql 表和数据注意:我只显示了两行的gift 列、id 和数据(希望没问题)

CREATE TABLE `products` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`gift` varchar(100) NOT NULL,

PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

INSERT INTO `products` VALUES(1,'him,her');
INSERT INTO `products` VALUES(2,'him,her,teens,gadgets');

如果产品在 mysql 表中只分配了一个值,这似乎有效,但当它有多个时则无效。从我读过的内容来看,我认为在单个列中存储多个标准可能会出错,但不知道如何解决它。非常感谢。

【问题讨论】:

  • 您可以编辑您的问题以包含您正在使用的数据库表中的一些示例吗?
  • 还在为 AJAX 使用原始 javascript 吗?考虑使用 jQuery!另外,停止使用已弃用的mysql_* 函数;使用 MySQLi 或 PDO。
  • 添加了数据库脚本,也感谢 Raptor,我改一下。任何想法为什么它不起作用?

标签: php mysql ajax multi-select


【解决方案1】:

你做错了。

首先,阅读数据库规范化。您必须有 3 张桌子。

type { id, title } for storing him,her,teens,gadgets as rows with unique id
product { id, product_title, ...... } for storing products
product_type {product_id, type_id} to store relation product -> type

您必须将选项数据作为数组发送:

index.php?options[1]=him&options[2]=her&options[3]=kids

然后在 PHP 中你得到它:

$options = $_GET['options'];

然后您可以使用连接构建查询:

$q = "SELECT a.id, a.product_title from product a JOIN product_type b ON a.a=b.product_id WHERE b.type_id IN('".implode("','",$options )."')";

附言 这只是一个例子。非常不安全。您必须需要为安全准备的语句、验证输入数据、创建正确的索引。您可以将“类型”存储在带有 ID 的数组中,但我更喜欢存储在数据库中。

您的示例在关系数据库设计方面是错误的。此外,使用 LIKE 是非常低效的。

【讨论】:

  • 谢谢,我知道一定有更合乎逻辑的方法。我试试这个。只是为了确认一下,在存储礼物选项的类型表中,礼物是否仍然存储在逗号分隔的列表中,还是应该每个都有自己的列?第三个表是否真的需要,或者只有前两个表才有可能?
  • 礼品选项存储为行!表格类型将只有 2 列:id、title
【解决方案2】:

好的,我找到了一个解决方案,但很确定它不是正确的方法,因为它不是很优雅。欢迎任何改进建议。我将explode $gift 变量设置为一个数组,然后生成几个带有通配符的OR LIKE 语句来匹配。看起来我的多选框中只有 9 个选项,因为这种方法很快就会变得乏味。

代码: 结果.PHP

$gift=$_GET['gift'];
$gift=str_replace("\\","",$gift);
$gift=str_replace("'","%",$gift);
$gift=explode(",",$gift);

$sql= "SELECT * 
FROM $productstable 
WHERE gift LIKE '$gift[0]' 
OR gift LIKE '$gift[1]' 
OR gift LIKE '$gift[2]' 
OR gift LIKE '$gift[3]' 
OR gift LIKE '$gift[4]' 
OR gift LIKE '$gift[5]' 
OR gift LIKE '$gift[6]' 
OR gift LIKE '$gift[7]' 
OR gift LIKE '$gift[8]'
OR gift LIKE '$gift[9]'";

$result = mysql_query($sql) or die ('Unable to run query:'.mysql_error());
while($item = mysql_fetch_array($result))
{
code to view products here
}
<? } mysql_close()  ?>

根本不喜欢这种方法,因为认为 IN 应该做同样的事情,但就是无法让它发挥作用。这会对速度产生负面影响吗?

稍后还将使用 PDO 重新编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2016-09-05
    相关资源
    最近更新 更多