【发布时间】:2013-07-03 05:51:28
【问题描述】:
我有一个带有多个复选框的 html 表单。我将这些传递给 php...
例如,这些值将像“G,BW”一样出现(“BW,G”也需要匹配) 在 check.php 中,我需要从 $_GET 中获取值并修改它们以进行 sql 查询...
<?php
if(!empty($_GET['wireColor'])) {
foreach($_GET['wireColor'] as $colors) {
echo $colors; //make sure it's right, then comment out
}
}
$colors = rtrim($colors, ','); //Get rid of trailing , - not working right
$wireSearch = '\'REGEXP \'.*(^|,).$wireColor.(,|$)\''; //Trying to add the sql bits to pass to the query.
理想情况下通过:
$colors_lookup_sql = "SELECT * FROM parts WHERE ( wire_colors REGEXP '.*(^|,)$wireSearch(,|$)' and wire_colors REGEXP '.*(^|,)$wireSearch(,|$)' );";
下面是查询在结尾的样子:
SELECT * FROM parts WHERE ( wire_colors REGEXP '.*(^|,)G(,|$)' and wire_colors REGEXP '.*(^|,)BW(,|$)' );
我很难将正则表达式位放入查询中。
更新
这是我现在拥有的:
<?php
if(!empty($_GET['wireColor'])) {
foreach($_GET['wireColor'] as $colors) {
$wireSearch = ' REGEXP \'.*(^|,)' .$colors.'(,|$)\' AND ';
}
}
$Search = rtrim($wireSearch, 'AND'); //Trying to trim the last "AND"
$colors_lookup_sql = "SELECT * FROM parts WHERE ( wire_colors $wireSearch% );";
这主要给了我我需要的东西,但是打印/回显结果,我得到了:
$wireSearch 最终结果为:REGEXP '.*(^|,)G(,|$)' AND REGEXP '.*(^|,)BW(,|$)' AND 这太棒了——我只需要核对最后一个“AND”。不过,上面的修剪将其替换为第二个值。很奇怪。
$colors_lookup_sql 最终为:SELECT * FROM parts WHERE ( wire_colors REGEXP '.*(^|,)BW(,|$)' AND % );
但由于某种原因,数组中的第一个值消失了,我不明白,因为它出现在 sql 语句之前。
【问题讨论】: