Order taking only the last registered product

Asked

Viewed 99 times

0

I am creating a simple system to make order reservations, where I bring the products from the database through a simple while, but if I try to put brackets in my Ames, example name=name[] it brings all products by clicking only on one, and if I take off the brackets it adds only the last product, regardless of which product I click.

Shopping Screen in which the user / customer selects the desired item to book.

<?php
    $stmt = $pdo->query('SELECT id, codigo, descricao, preco FROM produtos'); 
?>

<form method="POST" action="salvar_pedido.php" class="">  

        <div class="row">

            <?php while ($produtos = $stmt->fetchObject()){ ?>

                <div class="col-sm-6 col-md-4">

                    <div class="container">

                        <img style="width:75%;" src="imagem.php?id=<?php echo $produtos->id ?>" />

                        <div class="caption">
                                <input type="hidden" name="id" value="<?php echo $produtos->id ?>"> <br>
                                <input type="hidden" name="codigo" value="<?php echo $produtos->codigo ?>"> <br>
                                <strong>Descrição:</strong> <?php echo $produtos->descricao ?> <input type="hidden" name="descricao" value="<?php echo $produtos->descricao ?>">  <br>
                                <strong>Preço: </strong>  R$: <?php echo number_format($produtos->preco,2) ?> <input type="hidden" name="preco" value="<?php echo $produtos->preco ?>"> <br><br>
                                <input type="submit" class="btn btn-primary" value="Adicionar ao Pedido" id="comprar" name="comprar">
                        </div>
                        <br><br>
                    </div>

                </div>

            <?php } ?>

        </div>

</form>

save file.php

    <?php

        if (isset($_POST['comprar'])){

                $id = $_POST['id'];
                $pedido = $_POST['pedido'];
                $codigo = $_POST['codigo'];
                $descricao = $_POST['descricao'];
                $preco = $_POST['preco'];
                $status = $_POST['status'];

                $sql = "SELECT * FROM pedido  WHERE pedido = '$pedido'"; 
                $resulta = $conn->query($sql);
                $row = $resulta->fetch_assoc();

                $result = "INSERT INTO pedido (pedido, data_venda, codigo, descricao, preco_venda, status) VALUES ('$pedido', NOW(), '$codigo', '$descricao', '$preco', '$status')";
                $resultado = mysqli_query($conn, $result);   

                if($resultado){
                    echo "<script>redirectPedido()</script>";
                    echo "<script>alert('Pedido Realizado com Sucesso');</script>";
                }  else  {
                    echo "<script>redirectPedido()</script>";
                    echo "<script>alert('Erro ao Realizar Pedido');</script>";
                }

        }

    ?>

Whenever I click Submit add to the order it adds only the last product regardless of which item I select. Could someone help me ?

  • How $stmt was generated?

  • forgot @Sveen, I just added it above my FORM

  • 1

    Need an expensive form each set of inputs and submits or place the names with brackets to send everything at once as an array.

  • then the strange thing is that at the time of sending it does correctly but only takes the last product from the product table always, but in logic it should not occur this is not? Since I am informing the id inside the while, I thought I would get the respective id to each product

  • 1

    you only have one form, as all fields have the same name it sends only the last. To send all at once and with a form leave your fields like this: <input type="hidden" name="id[]" give a print_r($_POST); to see how the information was sent.

  • thanks, I had not thought of the array for this page, I will take the test and send the result here @rray

  • @rray tried in several ways, but without success, this way with brackets I step to pick up all the records in the while by clicking on only a single product

  • 1

    blz.... so when you click on Ubmit you just want to get the product id of that Ubmit that?

  • Yes I wanted to accumulate the products in a list referring to that user, in a way that he clicked on the Add to Order Submit and, he took the correct product to add to the order is only then the user would go to another page to view the order with each prpduto he added so he would just inform the quantity.

Show 4 more comments

1 answer

1


To solve this problem you should review the logic of creating your form.

In this code he makes a while printing several times fields with same ID, which causes the field to be duplicated and in Submit he always assumes the value of the last item.

Solution UP-TO-DATE with the most recent information included in the question:

For what has been indicated will be a SUBMIT button for each item to be added to the cart, so there should be a form for each item, as follows:

<?php
    $stmt = $pdo->query('SELECT id, codigo, descricao, preco FROM produtos'); 
?>
        <div class="row">
            <?php while ($produtos = $stmt->fetchObject()){ ?>
              <form method="POST" action="salvar_pedido.php" class="">  
                <div class="col-sm-6 col-md-4">
                    <div class="container">
                        <img style="width:75%;" src="imagem.php?id=<?php echo $produtos->id ?>" />
                        <div class="caption">
                                <input type="hidden" name="id" value="<?php echo $produtos->id ?>"> <br>
                                <input type="hidden" name="codigo" value="<?php echo $produtos->codigo ?>"> <br>
                                <strong>Descrição:</strong> <?php echo $produtos->descricao ?> <input type="hidden" name="descricao" value="<?php echo $produtos->descricao ?>">  <br>
                                <strong>Preço: </strong>  R$: <?php echo number_format($produtos->preco,2) ?> <input type="hidden" name="preco" value="<?php echo $produtos->preco ?>"> <br><br>
                                <input type="submit" class="btn btn-primary" value="Adicionar ao Pedido" id="comprar" name="comprar">
                        </div>
                        <br><br>
                    </div>
                </div>
              </form>
            <?php } ?>
        </div>

Previous answer considering that it would be a SUBMIT that would send all records:

You have two solutions: Create an array of "similar" fields like @rray said or an ID for each form field.

All that remains is to stop for a while and test the logic. Think of inputs as variables... if you put $code = 987, then put $code = 99999, at the end the variable will only have the value 99999 and not both.

  • in fact it brings the fields each one with its id, if I leave an input type text in the ID, it brings it correctly but when I select only search the ID of the last one that is strange to me

  • tried in several ways, but without success, this way with brackets I step to pick up all the records in the while by clicking on only a single product

  • See example of use in HTML and PHP to get all the data in the chosen answer (first) of this question asked here in stackoverflow: https://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array

  • If you’re going to use specific Ids when creating the form (already existing products, as it seems to be your case), explicitly put the input names with the associated Ids in the array or in an Hidden input... see what’s best for you.

  • See the new response based on the information indicated, maybe it is what you want... anything send message in the Box.

  • 1

    I got, it was exactly this, thanks for the help, I was vacillating about the form, using only one rs

Show 1 more comment

Browser other questions tagged

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