Search with explode and friendly url (problem)

1

I have a problem. I was using a search engine with variable multipules without a friendly url. And it worked normally. I placed several keywords and searched the database without any problem through EXPLODE.

Except now, I've placed a friendly URL and it's not working. What could it be? Detail: If I put just one word, it will work normally. Now if I put two words, the results will not appear.

Search form

    <div class="default-form-area">
            <form id="formBuscar" class="default-form" action="" method="GET">
                <div class="row clearfix">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <input type="text" name="search" id="search"  class="form-control" value="<?php if(isset($_GET["search"])) echo $_GET["search"]; ?>" placeholder="Escreva pelo menos uma palavra para realizar a busca... *" required="">
                        </div>
                    </div>
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                           <input type="submit" name="search" class="thm-btn thm-color" value="Buscar" />
                        </div>
                    </div>   
                </div>
            </form>
        </div>

<script>
$(function(){
   $("#formBuscar").submit(function(){
         var search = $("#search").val().toLowerCase();
         search = search.replace( " ", "+" );
         window.location = 'http://localhost/buscar/'+search;
      return false;
   });
});
</script>

Print search results:

  

Search results       

            <div class="table-responsive">  
                 <table class="table table-bordered">  
                 <?php

                 if(isset($_GET["search"]))  
                 {  
                      $condition = ''; 
                      $query = explode(" ", $_GET["search"]);                     
                      foreach($query as $text)  
                      {  

                      $db->query ('
                                    SELECT noticia_title FROM noticia WHERE noticia_title LIKE "%'.$text.'%" or noticia_content LIKE "%'.$text.'%"
                                    UNION
                                    SELECT evento_id FROM eventos WHERE evento_nome LIKE "%'.$text.'%" or evento_content LIKE "%'.$text.'%" or evento_resumo LIKE "%'.$text.'%"
                                    UNION
                                    SELECT album_id FROM albuns WHERE album_name LIKE "%'.$text.'%" or album_descricao LIKE "%'.$text.'%"  
                        ')->fetchAll();

                        if ($db->rows == 0) {
                            echo "<div class=\"alert info\">Nenhum resultado foi encontrado na sua busca!</div>";
                        } else {
                        if ($db->rows == 1) {
                            echo "<p align=\"center\">Durante sua busca foi encontrado 1 resultado em nosso site.</p><br />";
                        }
                        if ($db->rows > 1) {
                            echo "<p align=\"center\">Durante sua busca foi encontrado $db->rows resultados em nosso site.</p><br />";

                        }
                                }       
                 }

                ?>

Remembering: If I put in just one word, it will work normally. Now if I put two words, the results do not appear.

    
asked by anonymous 04.04.2018 / 19:10

1 answer

0

As discussed in the comments, the problem was in the file .htaccess which was like this:

RewriteRule ^buscar\/([a-z0-9-]+)/?$ index.php?p=buscar&search=$1 [QSA]

The regex used was not matching other characters passed in GET, but only letters from a to z , numbers, and hyphen.

Changing the regex to:

RewriteRule ^buscar\/([\w\W]+)/?$ index.php?p=buscar&search=$1 [QSA]

The problem has been resolved because it now matches any character passed in GET.

    
05.04.2018 / 01:17