-1

我正在修改 dolibarr 中的代码,并且在使用 WYSIG 编辑器(记录 html 文本)搜索产品时遇到了问题。搜索包含多个单词的描述在 a 之后
(可能是所有标签)或文本的开头不起作用,第一个单词不会返回描述。另外,“galvanisé”与“é”一起使用,但在“d'options”中与引号一起使用。见下面的例子:

我尝试使用 htmlentities 并包含标签的结束或开始(见下文)。

说明 :

过滤标准 avec tout un tas d'options 900x400x116
Nom 22
EN779-2002 acier
galvanisé

for( $i=0 ; $i < sizeof($tabMots) ; $i++) {
    if($i == 0) {
        if(is_numeric($tabMots[$i])) 
            $sql.= ' WHERE (p.description LIKE \'% '.$tabMots[0].' %\' OR p.description LIKE \'%>'.$tabMots[0].' %\' OR p.description LIKE \'%>'.$tabMots[0].'<%\' OR p.description LIKE \'% '.$tabMots[0].'<%\' )';
        else
            $sql.= ' WHERE (p.description LIKE \'% '.$tabMots[0].'%\' OR p.description LIKE \'% '.htmlentities($tabMots[0]).'%\' OR p.description LIKE \'%>'.$tabMots[0].'%\' OR p.description LIKE \'%>'.htmlentities($tabMots[0]).'%\' )';
    } else {
        if(is_numeric($tabMots[$i])) {
            $sql.= ' AND (p.description LIKE \'% '.$tabMots[$i].' %\'  OR p.description LIKE \'%>'.$tabMots[$i].'%\' OR p.description LIKE \'%>'.$tabMots[$i].' %\' OR p.description LIKE \'%>'.$tabMots[$i].'<%\' OR p.description LIKE \'% '.$tabMots[$i].'<%\' )';
        } else {
            $sql.= ' AND (p.description LIKE \'% '.$tabMots[$i].'%\'  OR p.description LIKE \'% '.htmlentities($tabMots[$i]).'%\' OR p.description LIKE \'%>'.$tabMots[$i].'%\' OR p.description LIKE \'%>'.htmlentities($tabMots[$i]).'%\' )';

        }
    }
}

我想避免添加纯文本字段,以免修改 dolibarr 数据库。谢谢。

编辑:建议的答案不好,因为问题是文本描述的文本格式 HTML,而不是搜索条件。

4

2 回答 2

0

对于那些不假思索就放 -1 的人,我想说准备好的查询并没有解决我的问题,因为它正在格式化搜索的单词。这里的问题是描述文本的格式,我进行了研究,发现插入
htmlentities() 后保留了 \n 和其他字符。但是,我不知道文本开头保留的字符。感谢您的帮助,但我已经实施了准备好的查询。
因此,当您希望 % 和单词 '%\n'.$word.'%' 之间有一个空格时,您必须添加此搜索以进行插入。在这里,您可以找到单词的词根 with
before。

于 2019-09-13T06:20:27.447 回答
0

认为您的错误将在于'登录词d'options转义 SQL 引擎中的字符串。

$tabMots[0] = "d'options";
$sql.= ' WHERE (p.description LIKE \'% '.$tabMots[0].'%\'';
echo $sql;

网上试试上面的

以上将打印出:

WHERE (p.description LIKE '% d'options%'

即使从 StackOverflow 高亮显示,您也可以看到 SQL 引擎从中获得了什么。

我强烈建议切换到准备好的语句。在这里阅读:

如何防止 PHP 中的 SQL 注入?

编辑

如何切换到准备好的语句?例子:

$param = $tabMots[$i];
$stmt = $db->prepare("SELECT id,Username FROM users WHERE Username LIKE CONCAT('%',?,'%') ");
$stmt->bind_param("s", $param);
$stmt->execute();

从这个答案复制粘贴:php mysqli Prepared statement LIKE

于 2019-09-12T07:14:16.010 回答