Mysql and PHP Search System - List Problems

Asked

Viewed 4,629 times

1

Hello, good night to all.

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

Before detailing the problem, I will specify how the system is.

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

I created a input for the search of the products, however, when I enter the name of a product, it duplicate.

<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 simplified the code because there are many lines considering the whole form, the PHP and the MySQL. I’m pretty sure the problem is because the codes PHP meet in different locations. I researched more about it and saw solutions that worked with OOP.

I intend to delve into this paradigm. However, I need your help to finish this work.

This is the problem: When searching, instead of just displaying what was requested, it duplicates the search and stays out of the layout (I think it’s because the codes PHP meet in different locations).

Forgive me for asking. Thanks in advance for your attention.

  • first try to put in the form the method Voce is using which in case would be <form method="POST"> . And then test the variable to search if it is even taking the value of the input field

  • hi, imagining that the code is correct, checks the places where you clean the $request and the $Pesquisar (( unset($request); or $request = ''; )) []’s

  • mysql_functions have already been removed from php7 so it is not appropriate to use them. Only with this snippet of code do you not notice the problem.

  • @Localhost, actually already found the syntax of POST in the application. Working up to the display. But I just need to exquisite the results, not duplicate them. Excuse me for not having put them in the example of the question.

  • @Art, truth, there was not thinking of zeroing the values. But this will work with codes php in different places?

  • @rray, I understand. I appreciate the guidance.

  • @Mikaelaraki you can demonstrate what happens using this website? This makes it easier to identify what is happening. Only with this piece of code, as already commented, we can not identify the error.

  • @Rafaelwithoeft, I’ll be seeing to it for you. Just a moment.

  • @Rafaelwithoeft, I won’t need more. I managed to solve with the help of a colleague. Thank you very much!

Show 4 more comments

1 answer

2


Found the solution to my problem!

Actually, I was concerned about something relatively simple. As I had done the PHP codes in different places, it 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 condition if 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 issue is of help to others who find themselves in the same situation. A tip (if not fundamental) is to look at the documentation of PHP to answer your questions about the language. : P

Thank you to everyone who tried to help me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.