在 html text in mysql field description without plain text 中搜索词
Search words in html text in mysql field description without plain text
我在 dolibarr 中修改代码,在使用 WYSIG 编辑器(记录 html 文本)描述产品时遇到了搜索问题。在
(可能是所有标签)之后或在文本的最开头,搜索包含多个单词的描述不起作用,第一个单词不是 return 描述。另外,"galvanisé" 与“é”一起使用,但不适用于带引号的 "d'options"。请参阅以下示例:
我尝试使用 html 实体并包含标签的结尾或开头(见下文)。
描述:
Filtre standard 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 而不是搜索标准。
我 认为 你的错误在于 '
登录词 d'options
转义了 SQL 引擎中的字符串.
$tabMots[0] = "d'options";
$sql.= ' WHERE (p.description LIKE \'% '.$tabMots[0].'%\'';
echo $sql;
上面会打印出这个:
WHERE (p.description LIKE '% d'options%'
即使从 Whosebug 突出显示,您也可以看到,SQL 引擎从中得到了什么。
我强烈建议切换到准备好的语句。在这里阅读:
How can I prevent SQL injection in PHP?
编辑
如何切换到准备好的语句?示例:
$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
对于那些不经思考就输入 -1 的人,我想说准备好的查询并没有解决我的问题,因为它正在格式化搜索的单词。这里的问题是描述文本的格式,我进行了研究,发现在插入
htmlentities() 之后
保留 \n 和其他字符。但是,我不知道文本开头的字符。感谢您帮助我实现了准备好的查询。所以,
当您想要在 % 和单词 '%\n'.$word.'%' 之间插入 space 时,您必须添加此搜索
。这里是词根前加
.