GET by passing all records in URL and POST Presents only the last record

Asked

Viewed 65 times

3

Good afternoon guys, all right ?

I’m trying to create a page to insert items inside a cart, this page keeps the client code and the command code in a variable and passes it between screens, on my product search screen I added a button, where clicking on it adds the product to that id I saved.

Problem: When I leave the form Action as GET, presents all the records on URL, now when I leave as POST, passes the value of the last database record to variable.

I believe there is some flaw in the logic created, could help me ?

NOTE: I left the product code inside the input to pass as POST;

<?php if($rows=mysqli_num_rows($result) > 0){ 

    ?>
    <form method="GET" action="incluir_itens.php">
        <!-- Criando os inputs abaixo para incluir o id da comanda e código do cliente, para enviar via POST pra incluir itens, não sei se é o melhor jeito, mas funcionou-->
        <input type="text" name="id_comanda" id="id_comanda" value="<?php echo $id_comanda;?>">
        <input type="text" name="cod_cliente" id="cod_cliente" value="<?php echo $cod_cliente;?>">

        <table id="tabela" class="table table-hover">
           <thead>
              <tr>
                 <th scope="col">Código</th>
                 <th scope="col">Descrição</th>
                 <th scope="col">Qtd</th>
                 <th scope="col">Preço de Venda</th>
                 <th scope="col"></th>
              </tr>
           </thead>
           <tbody>
              <?php
                 while($rows = mysqli_fetch_array($result)){ //while que mostra os resultados
                 ?>
              <tr>
                 <td>
                     <b><input type="text" name="cod_produto" id="cod_produto" value="<?php echo $rows['cod_produto']; ?>"></b>
                </td>
                    <div class="col-5">
                            <button type="submit" style="border:none;"><i class="fa fa-check" style="font-size:25px;color:green;"></i></button>
                    </div>

                 </td>
              </tr>
              <?php
                 }
                 mysqli_close($conn);
                 ?>
           </tbody>
        </table>
        </form>

URL as GET: /incluir_itens.php?id_comanda=32&cod_cliente=2&cod_produto=1&quantidade=&cod_produto=2&quantidade=&cod_produto=3&quantidade=&cod_produto=4&quantidade=&cod_produto=5&quantidade=

SQL Utilizado:

session_start();
   include('conexao.php');

   $tipo_pesquisa = $_POST['pesquisa_produto'];
   $valor_pesquisa = $_POST['valor_pesquisa'];



   if ($tipo_pesquisa == "descricao"){
       $sql_produtos = "select * from produtos where descricao like '%$valor_pesquisa%'";
   }
   elseif($tipo_pesquisa == "cod_produto"){
       $sql_produtos = "select * from produtos where cod_produto = $valor_pesquisa";
   }
   elseif($tipo_pesquisa == "cod_interno"){
       $sql_produtos = "select * from produtos where cod_interno = '$valor_pesquisa'";
   }

   $result=mysqli_query($conn, $sql_produtos);

Leave as POST, presents only the last record of my bank. If in my bank record there is only 1 record, I can include in the cart normally.

2 answers

0

This is because there are several inputs with the same name. Try to change:

<b><input type="text" name="cod_produto" id="cod_produto" value="<?php echo $rows['cod_produto']; ?>"></b>

To:

<b><input type="text" name="cod_produto[]" id="cod_produto" value="<?php echo $rows['cod_produto']; ?>"></b>
  • Opa Tiago, thanks for the help !! I tried this way, but keep pulling all the products. Inside [ ] I add some control variable or something like that ?

  • Edit the question and insert the SQL snippet.

  • Edited by Tiago ... Thank you !

  • Great, when you send PHP fields with the name contento [], you are saying that you are sending an array. In this case, you have to change your SQL: $sql_produtos = "select * from produtos where cod_produto IN (".implode(',', $valor_pesquisa).")";

  • Thanks Tiago for the return... What I find strange, is that the incident occurs only when I try to include the product in the cart, in case when the action is executed... Select itself is all right, but I believe that the problem is in the name of the fields, because everything is equal, system pulls the value of all fields... I will try to do what you did and also include a logic to change the "name" as lines, for example: Line 1: name="product code1" and so on. Because when select only brings 1 item, I can insert it into the cart normally.

  • Opa Tiago, I was checking and if I change the Button by a href, passes all the parameters right, only that line, but the action of the button sends the whole form, all records...

  • In HTML when you have an element button of the kind submit within the tags form, by clicking this form will be sent.

  • I’m glad you liked the answers! But the best way to thank those who helped you is to mark "accept" the best answer and vote for all who helped you. This way you make sure that whoever wrote the answer gets something in return, as well as making the site cleaner and more useful for everyone. Adding a new answer like this (which is not an answer to the question and should be removed) makes the site more confusing and can get in the way.

Show 3 more comments

0

I managed to solve. What happens is that as mentioned by Tiago, as I was sending a SUBMIT to the POST or GET, normal send the entire form.

My solution was the following, I created a link next to the line, pulling the code of the product, directing to a page where only presents that product and a field of quantity, so that ai yes I can execute the post that will send only that product.

Browser other questions tagged

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