Search in fields with HTML entities

0

I have a field in a MySQL database to which I have previously inserted HTML code; I also have a search engine that, when entering the word, searches for the results. The problem is that the data in HTML I have something like P & public that means Public, and when entering the text in the search engine it does not make me the like well.

For example, if I write pú, it stops me from doing the search because it can not pass the HTML code to the query and show the results.

My code is:

<?php

$conexion = new mysqli('localhost','','','');
if (mysqli_connect_error()) { echo mysqli_connect_error(); exit; }
    $q = "%" . $_POST['q'] . "%";

$consulta= $conexion->prepare("SELECT ar.Titulo_Articulo, ar.Id_Articulo, r.Id_Revista FROM articulos as ar INNER JOIN revista as r on r.Id_Revista = ar.Id_Revista where ar.pdf LIKE ?");
$consulta->bind_param("s", $q);
$consulta->execute();
$res = $consulta->get_result();


if($conexion->affected_rows>0)
{
  while($fila=$res->fetch_array())
    {

        echo '<a href="autenticacion.php?id='.$fila["Id_Revista"].'&articulo='.$fila["Id_Articulo"].'" width="50%" class="sugerencias" onclick="myFunction2('.utf8_encode($fila["Titulo_Articulo"]).')"><br>'.utf8_encode($fila['Titulo_Articulo']).'</a>';

    }
}
else
{
      echo '<b>No hay sugerencias</b>';
    }
    $consulta->close();
    $conexion->close();
?>

Hi, I update my question, the editor I use is tynimce, I copy and paste the data from a word document and when I insert them in the field, I get html code, what I need is that in the search engine I can perform the like in html, I used htmlentities and $ q="%" does not work. htmlentities ($ _ POST ['q']). "%";

Hello, I think I made a mistake the text is utf-8 not html, I can not put the post variable with html_entities because it is an input, this has to be in the output but what is it? No, I think that if it is in the input, the output has nothing to do since the insertion of data "pú the letter" or "must convert to utf-8 code, I've already used utf-8 decode and encode and nothing

In the end I have solved it, the problem is that tinymce has its documentation and I have put the encoding as raw link after I inserted the variable as htmlentities and I managed to search with htmlentities and at the end I did the search

    
asked by Quiroz Luis 29.04.2016 в 01:06
source

3 answers

1

To search with like you have to do the encoding of what you want to look for with htmlentities, as you have already been told:

 $q = "%" .htmlentities($_POST['q']) ."%"; 

Però it is wrong to save the data in html, it is better to convert everything before saving it, and if you need the html data you can create two fields, one for raw data and the other only with text for searches.

Another thing, if you want an 'autocomplete' it goes well like '%%', but be careful that it is very slow. If you want to do a search, it is better to create a text-only column and index it in FULL TEXT format, in order to search more efficiently and more quickly:

SELECT ar.Titulo_Articulo, ar.Id_Articulo, r.Id_Revista FROM articulos as ar
 INNER JOIN revista as r on r.Id_Revista = ar.Id_Revista 
 WHERE MATCH (ar.pdf) AGAINST (? IN NATURAL LANGUAGE MODE);

in this way you can also use * to complete the words in this form: 'Públi *'

    
answered by 29.04.2016 в 11:18
0

If you have the data saved as HTML entities in the database, be sure to convert that data to HTML entities before passing them to your query, it can be worth doing:

$q = "%" . htmlentities($_POST['q']) . "%";

Although, as other colleagues have told you, it is advisable to use UTF8 encoding in these cases, which use data formatted as HTML.

    
answered by 29.04.2016 в 10:51
0

Luis, call me a little attention to save the data in html format, if you need to perform a search on them; in this case you would have to implement that when looking for the word, it is previously converted as an html entity using htmlentities () , so that the query works in this way.

I mention this because there is no function in MySQL to encode / decode the html code.

    
answered by 29.04.2016 в 02:33