PHP and MySQL Search Engine - Listing Problems

1

Hello, good evening, everyone.

This is my first question on the forum and I would like to resolve a problem with my small web application. This is a final semester work of my course, and I need to create a small system of product registration, using the functions of Insert, Delete, Change and List with PHP and MySQL .

Before detailing the problem, I'll specify how the system is.

When you register a new product, a modal is opened, and then enter all data for the product. After inserting, the registered product is added to div , where it contains a box with the image and product information.

I've created a input for product search, but when I enter the name of a product, it doubles .

<div id="Conteudo">

  <form>    
    <input id="Pesquisa" type="search" name="Pesquisar">
    <input id="Buscar" type="submit" name="Enviar" />    
    <!-- Código PHP -->    
  </form>
  
  <div id="Produto">    
    <!--  Descrição do produto -->
    <!-- Código PHP para exibir Produto após seu registro -->
  </div>

</div>

<!-- Código PHP que realiza a busca no banco e lista o resultado -->
<?php
  if(isset($_POST['Enviar'])){
    $Pesquisar = $_POST['Pesquisar'];
    if($Pesquisar != NULL){
      $request = mysql_query("SELECT * FROM 'banco'.'tabela' WHERE nome = '$Pesquisar' ");
      echo "
        <div id="Produto">
          <!--  Conteúdo  -->
        </div>
      ";
?>

I have simplified the code since there are many lines considering the entire form, PHP and MySQL . I'm pretty sure the problem is because the PHP codes are in different places. I came to research more about it and saw solutions that worked with OOP .

I want to delve deeper into this paradigm. However, I need your help in completing this work.

This is the problem: When searching, instead of just displaying what was requested, it duplicates the search and gets out of the layout (I think that% different locations).

Please forgive me the length of the question. Thanks in advance for your attention.

    
asked by anonymous 23.06.2016 / 05:24

1 answer

1

Found the solution to my problem!

Actually, I was worried about something relatively simple. Since I had made PHP codes in different places, this made it impossible for me to change the values of the variables.

<!-- Código de Exibir os registros -->
<?php
  #  Código PHP
?>
<!-- Código de Exibir os resultados da pesquisa -->
<?php
  # Outro código PHP
?>

<!-- Por estarem em diferentes locais,
não era possível mudar os valores de uma variável
de um PHP atráves do outro -->

So I just unified the two codes and put a if condition to display the results.

<?php
  # CAMPO DE PESQUISA
  if(isset($_POST['send'])){
    $search = $_POST['search'];
    if($search != NULL){
      # VERIFICA NO BANCO TODOS OS DADOS QUE POSSUEM O NOME PRÓXIMO AO QUE FOI DIGITADO NO CAMPO DE PESQUISA
      $request = mysql_query("SELECT * FROM 'tabela' WHERE 'campo' LIKE '%".$search."%' ");
      if($request != 0){
        # ARMAZENA A REQUISIÇÃO
        while($return = mysql_fetch_aray($request)){
          #  EXIBIR RESULTADOS DA BUSCA
        }
      }
    }
  } else {
      # SE NÃO FOI EXECUTADO A FUNÇÃO DE PESQUISAR, EXECUTE ESSA FUNÇAO DE EXIBIR TODOS OS REGISTROS
      $request = mysql_query("SELECT * FROM 'tabela' ORDER BY 'id' DESC");
      while ($return = mysql_fetch_array($request)){
        #  EXIBIR TODOS OS REGISTROS
      }
  }
?>

That this question is of help to others who find themselves in the same situation. One tip (if not fundamental) is to look at the documentation of PHP to get your doubts about language. : P

Thanks to everyone who has tried to help me.

    
26.06.2016 / 08:05